Skip to content

Commit

Permalink
feat: persist shares in db
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 25, 2024
1 parent 15de7ac commit 57cd115
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/shares/.env.manual
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NODE_ENV='development'
SHARES_GRPC_URL='0.0.0.0:4070'
DATABASE_URL=mongodb://bs:password@mongodb:27017
2 changes: 2 additions & 0 deletions apps/shares/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './shares.repository';
export * from './shares.schema';
17 changes: 17 additions & 0 deletions apps/shares/src/db/shares.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Injectable, Logger } from '@nestjs/common';
import { AbstractRepository } from '@bitsacco/common';
import { SharesDocument } from './shares.schema';

@Injectable()
export class SharesRepository extends AbstractRepository<SharesDocument> {
protected readonly logger = new Logger(SharesRepository.name);

constructor(
@InjectModel(SharesDocument.name)
reservationModel: Model<SharesDocument>,
) {
super(reservationModel);
}
}
13 changes: 13 additions & 0 deletions apps/shares/src/db/shares.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { AbstractDocument } from '@bitsacco/common';

@Schema({ versionKey: false })
export class SharesDocument extends AbstractDocument {
@Prop({ type: String, required: true })
userId: string;

@Prop({ type: Number, required: true })
quantity: number;
}

export const SharesSchema = SchemaFactory.createForClass(SharesDocument);
10 changes: 8 additions & 2 deletions apps/shares/src/shares.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Joi from 'joi';
import { Module } from '@nestjs/common';
import { LoggerModule } from '@bitsacco/common';
import { DatabaseModule, LoggerModule } from '@bitsacco/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SharesController } from './shares.controller';
import { SharesService } from './shares.service';
import { SharesDocument, SharesRepository, SharesSchema } from './db';

@Module({
imports: [
Expand All @@ -12,11 +13,16 @@ import { SharesService } from './shares.service';
validationSchema: Joi.object({
NODE_ENV: Joi.string().required(),
SHARES_GRPC_URL: Joi.string().required(),
DATABASE_URL: Joi.string().required(),
}),
}),
DatabaseModule,
DatabaseModule.forFeature([
{ name: SharesDocument.name, schema: SharesSchema },
]),
LoggerModule,
],
controllers: [SharesController],
providers: [SharesService, ConfigService],
providers: [SharesService, ConfigService, SharesRepository],
})
export class SharesModule {}
29 changes: 21 additions & 8 deletions apps/shares/src/shares.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Injectable, Logger } from '@nestjs/common';
import { BuySharesDto, ShareDetailResponse } from '@bitsacco/common';
import { SharesRepository } from './db';

@Injectable()
export class SharesService {
private readonly logger = new Logger(SharesService.name);

constructor() {
constructor(private readonly shares: SharesRepository) {
this.logger.log('SharesService created');
}

Expand All @@ -15,15 +16,27 @@ export class SharesService {
}: BuySharesDto): Promise<ShareDetailResponse> {
this.logger.debug(`Buying ${quantity} Bitsacco shares for ${userId}`);

await this.shares.create({
userId,
quantity,
});

const allShares = await this.shares.find({ userId });
const totalShares = allShares.reduce(
(sum, share) => sum + share.quantity,
0,
);
const shares = allShares
.map((share) => ({
quantity: share.quantity,
purchasedAtUnix: Number(share.createdAt),
}))
.reverse();

const res: ShareDetailResponse = {
userId,
totalShares: quantity,
shares: [
{
quantity,
purchasedAtUnix: new Date().getTime(),
},
],
totalShares,
shares,
};

return Promise.resolve(res);
Expand Down
1 change: 0 additions & 1 deletion libs/common/src/database/abstract.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { v4 as uuidv4 } from 'uuid';
import { SchemaTypes } from 'mongoose';
import { Prop, Schema } from '@nestjs/mongoose';
import { UUID } from 'crypto';

@Schema()
export class AbstractDocument {
Expand Down

0 comments on commit 57cd115

Please sign in to comment.