-
Notifications
You must be signed in to change notification settings - Fork 1
Added an endpoint to add coins to the clan #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MikhailDeriabin
merged 11 commits into
dev
from
feature/add-a-endpoint-to-add-coins-to-the-clan-550
May 23, 2025
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7f35689
Add shop module and default clanCoins controller
jkrizsan c96e89d
Add clanCoins enum and DTO. Apply them in the ClanCoins controller. …
jkrizsan c6d99c7
Add ClanCoinService. Add necessary changes to the Dtos and shop modul…
jkrizsan 3a33798
Add shop testing module. add clanCoinsService.addCoins() test suite
jkrizsan 807efde
coins enum: fix typo
jkrizsan 812d290
Took awai gameCoins from UpdateClan dto and from UpdateClanDtoBuilder
jkrizsan 5247139
Refactor a bit clanCoins.service
jkrizsan 3c8d0a6
clanCoins.service use inc operator
jkrizsan bbec716
clanCoins.dto: drop clan_id
jkrizsan 1fd5fac
clanCoins.service: use more params instead of request dto
jkrizsan a4168e5
addCoins.test: drop unnecessary test
jkrizsan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import ClanBuilderFactory from '../../../../__tests__/clan/data/clanBuilderFactory'; | ||
| import ClanModule from '../../../clan/modules/clan.module'; | ||
| import ClanCoinsModule from '../../modules/clanCoins.module'; | ||
| import { ClanCoinsService } from '../../../../shop/buy/clanCoins.service'; | ||
| import { Coins } from '../../../../shop/enum/coins.enum.dto'; | ||
| import ClanCoinsBuilderFactory from '../../data/clanCoinsBuilderFactory'; | ||
| import { ClanCoinsDto } from '../../../../shop/buy/dto/clanCoins.dto'; | ||
|
|
||
| describe('clanCoinsService.addCoins() test suite', () => { | ||
|
|
||
| const clanModel = ClanModule.getClanModel(); | ||
| const clanBuilder = ClanBuilderFactory.getBuilder('Clan'); | ||
|
|
||
| let clanCoinsService: ClanCoinsService; | ||
| let clanCoins: ClanCoinsDto; | ||
|
|
||
| const clan_id = '681e534624e7710f1b5ccb80'; | ||
| const coins = Coins.FiveHundred; | ||
|
|
||
| beforeEach(async () => { | ||
| clanCoinsService = await ClanCoinsModule.getClainCoinsService(); | ||
|
|
||
| const clanCoinsBuilder = ClanCoinsBuilderFactory.getBuilder('ClanCoinsDto'); | ||
|
|
||
| clanCoins = clanCoinsBuilder | ||
| .setId(clan_id) | ||
| .setAmount(coins) | ||
| .build(); | ||
| }); | ||
|
|
||
| it('Should add the amount to the Clans gameCoins if input is valid', async () => { | ||
|
|
||
| const clan = clanBuilder | ||
| .setName('clan1') | ||
| .setId(clan_id) | ||
| .build(); | ||
|
|
||
| await clanModel.create(clan); | ||
|
|
||
| await clanCoinsService.addCoins(clanCoins); | ||
|
|
||
| const dbResp = await clanModel.find({ _id: clan_id }); | ||
| expect(dbResp[0].gameCoins).toBe(coins); | ||
| }); | ||
|
|
||
| it('Should return with error if the clan does not exist', async () => { | ||
|
|
||
| const [_, error] = await clanCoinsService.addCoins(clanCoins); | ||
|
|
||
| expect(error).toBeDefined(); | ||
| expect(error[0]?.reason).toBe('NOT_FOUND'); | ||
| expect(error[0]?.field).toBe('_id'); | ||
| expect(error[0]?.value).toBe(clan_id); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { ObjectId } from 'mongodb'; | ||
| import IDataBuilder from '../../../test_utils/interface/IDataBuilder'; | ||
| import { ClanCoinsDto } from '../../../../shop/buy/dto/clanCoins.dto'; | ||
| import { Coins } from '../../../../shop/enum/coins.enum.dto'; | ||
|
|
||
| export default class ClanCoinsDtoBuilder implements IDataBuilder<ClanCoinsDto> { | ||
| private readonly base: ClanCoinsDto = { | ||
| clan_id: 'clan-id', | ||
| amount: Coins.FiveHundred, // or another valid Coins enum value | ||
| }; | ||
|
|
||
| build(): ClanCoinsDto { | ||
| return { ...this.base }; | ||
| } | ||
| setId(id: string | ObjectId) { | ||
| this.base.clan_id = id as any; | ||
| return this; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/__tests__/shop/data/clanCoins/createClanCoinsDtoBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { ObjectId } from 'mongodb'; | ||
| import IDataBuilder from '../../../test_utils/interface/IDataBuilder'; | ||
| import { ClanCoinsDto } from '../../../../shop/buy/dto/clanCoins.dto'; | ||
| import { Coins } from '../../../../shop/enum/coins.enum.dto'; | ||
|
|
||
| export default class ClanCoinsDtoBuilder | ||
| implements IDataBuilder<ClanCoinsDto> | ||
| { | ||
| private readonly base: ClanCoinsDto = { | ||
| clan_id: '', | ||
| amount: Coins.OneHundred | ||
| }; | ||
|
|
||
| build(): ClanCoinsDto { | ||
| return { ...this.base }; | ||
| } | ||
|
|
||
| setId(id: string | ObjectId) { | ||
| this.base.clan_id = id as any; | ||
| return this; | ||
| } | ||
|
|
||
| setAmount(amount: Coins) { | ||
| this.base.amount = amount; | ||
| return this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import ClanCoinsDtoBuilder from "./clanCoins/createClanCoinsDtoBuilder"; | ||
|
|
||
| type BuilderName = | ||
| | 'ClanCoinsDto'; | ||
|
|
||
| type BuilderMap = { | ||
| ClanCoinsDto: ClanCoinsDtoBuilder; | ||
| }; | ||
|
|
||
| export default class PlayerBuilderFactory { | ||
| static getBuilder<T extends BuilderName>(builderName: T): BuilderMap[T] { | ||
| switch (builderName) { | ||
| case 'ClanCoinsDto': | ||
| return new ClanCoinsDtoBuilder() as BuilderMap[T]; | ||
| default: | ||
| throw new Error(`Unknown builder name: ${builderName}`); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import ClanCoinsCommonModule from './clanCoinsCommon'; | ||
| import { ClanCoinsService } from '../../../shop/buy/clanCoins.service'; | ||
|
|
||
| export default class ClanCoinsModule { | ||
| private constructor() {} | ||
| static async getClainCoinsService() { | ||
| const module = await ClanCoinsCommonModule.getModule(); | ||
| return module.resolve(ClanCoinsService); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { MongooseModule } from '@nestjs/mongoose'; | ||
| import { ModelName } from '../../../common/enum/modelName.enum'; | ||
| import { mongooseOptions, mongoString } from '../../test_utils/const/db'; | ||
| import { ClanSchema } from '../../../clan/clan.schema'; | ||
| import { ClanModule } from '../../../clan/clan.module'; | ||
| import { ClanCoinsService } from '../../..//shop/buy/clanCoins.service'; | ||
| import { ShopModule } from '../../../shop/shop.module'; | ||
|
|
||
| export default class ClanCoinsCommonModule { | ||
| private constructor() {} | ||
|
|
||
| private static module: TestingModule; | ||
|
|
||
| static async getModule() { | ||
| if (!ClanCoinsCommonModule.module) | ||
| ClanCoinsCommonModule.module = await Test.createTestingModule({ | ||
| imports: [ | ||
| MongooseModule.forRoot(mongoString, mongooseOptions), | ||
| MongooseModule.forFeature([ | ||
| { name: ModelName.CLAN, schema: ClanSchema }, | ||
| ]), | ||
| ClanModule, | ||
| ShopModule | ||
| ], | ||
| providers: [ | ||
| ClanCoinsService | ||
| ], | ||
| }).compile(); | ||
|
|
||
| return ClanCoinsCommonModule.module; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { | ||
| Body, | ||
| Controller, | ||
| HttpCode, | ||
| Post, | ||
| } from '@nestjs/common'; | ||
|
|
||
| import { LoggedUser } from '../../common/decorator/param/LoggedUser.decorator'; | ||
| import { ClanCoinsDto } from './dto/clanCoins.dto'; | ||
| import { User } from '../../auth/user'; | ||
| import DetermineClanId from '../../common/guard/clanId.guard'; | ||
| import { ClanCoinsService } from './clanCoins.service'; | ||
|
|
||
| @Controller('clanCoins') | ||
| export class ClanCoinsController { | ||
| public constructor(private readonly service: ClanCoinsService,) {} | ||
|
|
||
| @Post() | ||
| @HttpCode(204) | ||
| @DetermineClanId() | ||
| public async addCoins(@Body() body: ClanCoinsDto, @LoggedUser() user: User) { | ||
|
|
||
| body.clan_id = user.clan_id; | ||
|
|
||
| const [, errors] = await this.service.addCoins(body); | ||
| if (errors) return [null, errors]; | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ClanService } from '../../clan/clan.service'; | ||
| import { ClanCoinsDto } from './dto/clanCoins.dto'; | ||
| import { UpdateClanDto } from '../../clan/dto/updateClan.dto'; | ||
|
|
||
| @Injectable() | ||
| export class ClanCoinsService { | ||
| public constructor( | ||
| private readonly clanService: ClanService, | ||
| ) {} | ||
|
|
||
| async addCoins( | ||
| body: ClanCoinsDto | ||
| ) { | ||
| const [clan, error] = await this.clanService.readOneById(body.clan_id); | ||
| if (error) return [null, error]; | ||
|
|
||
| const updateClanDto = new UpdateClanDto(); | ||
| updateClanDto._id = body.clan_id; | ||
| updateClanDto.gameCoins = clan.gameCoins + body.amount; | ||
|
|
||
| return await this.clanService.updateOneById(updateClanDto); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { | ||
| IsEnum, | ||
| IsMongoId, | ||
| IsOptional, | ||
| } from 'class-validator'; | ||
| import AddType from '../../../common/base/decorator/AddType.decorator'; | ||
| import { Coins } from '../../../shop/enum/coins.enum.dto'; | ||
| import { IsClanExists } from '../../../clan/decorator/validation/IsClanExists.decorator'; | ||
|
|
||
| @AddType('ClanCoinsDto') | ||
| export class ClanCoinsDto { | ||
|
|
||
| @IsClanExists() | ||
| @IsMongoId() | ||
| @IsOptional() | ||
| clan_id: string; | ||
PlayJeri marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @IsEnum(Coins) | ||
| amount: Coins; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export enum Coins { | ||
| OneHundred = 100, | ||
| TwoHundredFifty = 250, | ||
| FiveHundred = 500, | ||
| OneThousand = 1000, | ||
| TwoThousands = 2000, | ||
| FiveThousands = 5000, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { MongooseModule } from '@nestjs/mongoose'; | ||
| import { ClanCoinsController } from './buy/clanCoins.controller'; | ||
| import { ModelName } from '../common/enum/modelName.enum'; | ||
| import { ClanSchema } from '../clan/clan.schema'; | ||
| import { joinSchema } from '../clan/join/join.schema'; | ||
| import { PlayerSchema } from '../player/schemas/player.schema'; | ||
| import { ClanInventoryModule } from '../clanInventory/clanInventory.module'; | ||
| import { RequestHelperModule } from '../requestHelper/requestHelper.module'; | ||
| import { PlayerModule } from '../player/player.module'; | ||
| import { GameEventsEmitterModule } from '../gameEventsEmitter/gameEventsEmitter.module'; | ||
| import ClanHelperService from '../clan/utils/clanHelper.service'; | ||
| import { ClanCoinsService } from './buy/clanCoins.service'; | ||
| import { ClanService } from '../clan/clan.service'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| MongooseModule.forFeature([ | ||
| { name: ModelName.CLAN, schema: ClanSchema }, | ||
| { name: ModelName.JOIN, schema: joinSchema }, | ||
| { name: ModelName.PLAYER, schema: PlayerSchema }, | ||
| ]), | ||
| ClanInventoryModule, | ||
| RequestHelperModule, | ||
| PlayerModule, | ||
| GameEventsEmitterModule, | ||
| ], | ||
| controllers: [ClanCoinsController], | ||
| providers: [ | ||
| ClanService, | ||
| ClanCoinsService, | ||
| ClanHelperService, | ||
| ], | ||
| exports: [], | ||
| }) | ||
| export class ShopModule {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field can not be here, because that means that the gameCoins can be updated straight from the /clan PUT endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that's the job of the /clan PUT endpoint to update the clan properties :)
Anyway, I dropped the extra feature...