Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/__tests__/clan/data/clan/UpdateClanDtoBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class UpdateClanDtoBuilder {
phrase: undefined,
language: undefined,
clanLogo: undefined,
gameCoins: 0
};

build() {
Expand Down Expand Up @@ -78,4 +79,9 @@ export default class UpdateClanDtoBuilder {
this.base.language = language;
return this;
}

setGameCoins(gameCoins: number) {
this.base.gameCoins = gameCoins;
return this;
}
}
55 changes: 55 additions & 0 deletions src/__tests__/shop/buy/ClanCoinsService/addCoins.test.ts
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);
});
});
19 changes: 19 additions & 0 deletions src/__tests__/shop/data/clanCoins/clanCoinsDtoBuilder.ts
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 src/__tests__/shop/data/clanCoins/createClanCoinsDtoBuilder.ts
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;
}
}
19 changes: 19 additions & 0 deletions src/__tests__/shop/data/clanCoinsBuilderFactory.ts
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}`);
}
}
}
10 changes: 10 additions & 0 deletions src/__tests__/shop/modules/clanCoins.module.ts
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);
}
}
33 changes: 33 additions & 0 deletions src/__tests__/shop/modules/clanCoinsCommon.ts
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;
}
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import isTestingSession from './box/util/isTestingSession';
import { ScheduleModule } from '@nestjs/schedule';
import { OnlinePlayersModule } from './onlinePlayers/onlinePlayers.module';
import { ClanShopModule } from './clanShop/clanShop.module';
import { ShopModule } from './shop/shop.module';

// Set up database connection
const mongoUser = envVars.MONGO_USERNAME;
Expand Down Expand Up @@ -86,6 +87,7 @@ const authGuardClassToUse = isTestingSession() ? BoxAuthGuard : AuthGuard;
BoxModule,
OnlinePlayersModule,
ClanShopModule,
ShopModule,
],
controllers: [AppController],
providers: [
Expand Down
3 changes: 3 additions & 0 deletions src/clan/dto/updateClan.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ export class UpdateClanDto {
@IsEnum(Language)
@IsOptional()
language?: Language;

Copy link
Collaborator

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.

Copy link
Contributor Author

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...

@IsOptional()
gameCoins: number;
}
29 changes: 29 additions & 0 deletions src/shop/buy/clanCoins.controller.ts
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];

}
}
24 changes: 24 additions & 0 deletions src/shop/buy/clanCoins.service.ts
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);
}
}
20 changes: 20 additions & 0 deletions src/shop/buy/dto/clanCoins.dto.ts
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;

@IsEnum(Coins)
amount: Coins;
}
8 changes: 8 additions & 0 deletions src/shop/enum/coins.enum.dto.ts
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,
}
36 changes: 36 additions & 0 deletions src/shop/shop.module.ts
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 {}