Skip to content

Commit

Permalink
Add sharesUpdatedAt field (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv authored Aug 29, 2023
1 parent 5facc89 commit 6fff8c1
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 7 deletions.
118 changes: 118 additions & 0 deletions migrations/20230829104441-add-shares-updated-at-to-positions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module.exports = {
async up(db) {
await db
.collection('positions')
.updateOne(
{ uuid: '854c8391-80df-49cc-8451-68295cc532cd' },
{ $set: { sharesUpdatedAt: new Date('2023-05-30') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'af621e70-9b51-43b4-a774-ae93a164948b' },
{ $set: { sharesUpdatedAt: new Date('2023-08-24') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'fb7b4c7c-901a-4daa-9b6f-6c1e83170506' },
{ $set: { sharesUpdatedAt: new Date('2023-04-11') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '2681874c-2075-462f-ab4b-3315d4f0629c' },
{ $set: { sharesUpdatedAt: new Date('2023-05-02') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '3b952edc-aa85-4913-aa9d-839720ef53d3' },
{ $set: { sharesUpdatedAt: new Date('2023-08-24') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '2283c61a-3b9f-4af1-a665-c202b6109606' },
{ $set: { sharesUpdatedAt: new Date('2023-08-01') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'd6c3208e-4b52-4b61-96c8-19adceae9f9e' },
{ $set: { sharesUpdatedAt: new Date('2023-04-05') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '52ebed0e-419f-45e6-bceb-a9eaef243cdd' },
{ $set: { sharesUpdatedAt: new Date('2023-08-24') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'a24988f5-7e97-413e-9817-f563b6de6be7' },
{ $set: { sharesUpdatedAt: new Date('2023-05-30') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'd149ac82-1421-45ae-990e-32173e0b1d86' },
{ $set: { sharesUpdatedAt: new Date('2023-08-14') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '6552ca2e-a7dd-4490-aa85-798a3e3f39bc' },
{ $set: { sharesUpdatedAt: new Date('2023-08-08') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'acdb68f8-ccc4-41ac-a3d7-c6780a356b28' },
{ $set: { sharesUpdatedAt: new Date('2023-08-07') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: 'dd800208-f1b2-4c8b-a304-840cb7a7b090' },
{ $set: { sharesUpdatedAt: new Date('2023-07-05') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '6085e3bf-14b5-4e2f-8b10-de15484fa04f' },
{ $set: { sharesUpdatedAt: new Date('2023-08-07') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '275be5c8-d777-40dc-89ef-dfeadaa0f5bd' },
{ $set: { sharesUpdatedAt: new Date('2023-08-14') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '231aed8e-a3bd-4bc8-8c32-bc9ee73593eb' },
{ $set: { sharesUpdatedAt: new Date('2023-05-23') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '413fa5f1-5ab2-4244-84e9-aa5a8f415861' },
{ $set: { sharesUpdatedAt: new Date('2023-08-24') } },
);
await db
.collection('positions')
.updateOne(
{ uuid: '33edc4ba-2d9a-45ed-8136-6f6f60274f74' },
{ $set: { sharesUpdatedAt: new Date('2023-08-18') } },
);
},

async down(db) {
await db
.collection('positions')
.updateMany({}, { $unset: { sharesUpdatedAt: true } });
},
};
5 changes: 2 additions & 3 deletions src/companies/domain/companies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ export class CompaniesService implements OnApplicationBootstrap {
throw new NotFoundException('Company not found');
}

const state = await this.companyStatesService.getLastStateByCompanyUuid(
uuid,
);
const state =
await this.companyStatesService.getLastStateByCompanyUuid(uuid);

return <CompanyWithState>{ ...company, state };
}
Expand Down
2 changes: 1 addition & 1 deletion src/portfolios/__tests__/portfolios.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Portfolios e2e tests', () => {
});
});

it('/POST portfolio, company and two positions', async () => {
it.only('/POST portfolio, company and two positions', async () => {
await request(app.getHttpServer())
.post('/portfolios')
.set('Authorization', `Bearer ${accessToken}`)
Expand Down
1 change: 1 addition & 0 deletions src/portfolios/domain/dto/position-detail.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export class PositionDetailDto {
deltaShares: number;
companyState: CompanyState;
blocked: boolean;
sharesUpdatedAt: Date | null;
}
1 change: 1 addition & 0 deletions src/portfolios/domain/entities/position.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export class Position {
symbol: string;
value: number;
blocked: boolean;
sharesUpdatedAt: Date;
}
5 changes: 2 additions & 3 deletions src/portfolios/domain/portfolios.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ export class PortfoliosService implements OnApplicationBootstrap {

const positions =
await this.positionService.getPositionDetailsByPortfolioUuid(portfolio);
const state = await this.portfolioStatesService.getLastByPortfolioUuid(
uuid,
);
const state =
await this.portfolioStatesService.getLastByPortfolioUuid(uuid);

return <PortfolioDetailDto>{
uuid,
Expand Down
6 changes: 6 additions & 0 deletions src/portfolios/domain/positions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class PositionsService {
uuid,
companyUuid: company.uuid,
blocked: false,
sharesUpdatedAt: new Date(),
});

const created = await this.repository.findByUuid(uuid);
Expand Down Expand Up @@ -106,6 +107,10 @@ export class PositionsService {
companyUuid: company.uuid,
symbol: upsertPositionDto.symbol,
blocked: upsertPositionDto.blocked ?? false,
sharesUpdatedAt:
upsertPositionDto.shares !== existentPosition.shares
? new Date()
: existentPosition.sharesUpdatedAt,
});

const updated = await this.repository.findByUuid(existentPosition.uuid);
Expand Down Expand Up @@ -178,6 +183,7 @@ export class PositionsService {
targetWeight: position.targetWeight,
blocked: position.blocked,
companyState,
sharesUpdatedAt: position.sharesUpdatedAt ?? null,
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/portfolios/repositories/schemas/position.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class PositionModel {

@Prop({ required: true })
blocked: boolean;

@Prop({ required: true })
sharesUpdatedAt: Date;
}

export const PositionSchema = SchemaFactory.createForClass(PositionModel);
2 changes: 2 additions & 0 deletions src/portfolios/routes/entities/position.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export class Position implements DomainPosition {
value: number;
@ApiProperty()
blocked: boolean;
@ApiProperty()
sharesUpdatedAt: Date;
}

0 comments on commit 6fff8c1

Please sign in to comment.