Skip to content

Commit

Permalink
feat: add affiliation endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree committed Oct 5, 2023
1 parent 1401b02 commit b12a9af
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 0 deletions.
4 changes: 4 additions & 0 deletions services/character-sheet/src/data/affiliations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,7 @@ export namespace Affiliation {
],
};
}

export const AffiliationIds = Object.keys(Affiliation);

export type AffiliationId = keyof typeof Affiliation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Prop, Schema } from '@nestjs/mongoose';
import {
IsInt, Min, Max, IsEnum,
} from '@cats-cradle/validation-schemas';
import { AffiliationId, AffiliationIds } from '../data/affiliations';

@Schema({ _id: false })
export class AffiliationEmbeddable {
@IsEnum(AffiliationIds)
@Prop()
public affiliationId!: AffiliationId;

@IsInt()
@Min(0)
@Max(255)
@Prop()
public amount!: number;
}
8 changes: 8 additions & 0 deletions services/character-sheet/src/models/character-sheet.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@cats-cradle/validation-schemas';
import { v4 as uuidv4 } from 'uuid';
import { DisciplineEmbeddable } from './discipline-embeddable.schema';
import { AffiliationEmbeddable } from './affiliation-embeddable.schema';
import { StatsEmbeddable } from './stats-embeddable.schema';
import { EquipmentEmbeddable } from './equipment-embeddable.schema';
import { GaugeEmbeddable } from './gauge-embeddable.schema';
Expand Down Expand Up @@ -106,6 +107,13 @@ export class CharacterSheet {
@ArrayMaxSize(12)
@Prop()
public equipment: EquipmentEmbeddable[];

@ValidateNested({ each: true })
@Type(() => AffiliationEmbeddable)
@IsArray()
@ArrayUnique()
@Prop()
public affiliation: AffiliationEmbeddable[];
}

export type TCharacterSheetDocument = CharacterSheet & Document;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
Controller, Get, Param, VERSION_NEUTRAL,
} from '@nestjs/common';
import { AffiliationService } from './affiliation.service';
import { QueryAffiliationDto } from './query-affiliation.dto';
import { CharacterSheetRepository } from '../../models/character-sheet.repository';

@Controller({ path: '/affiliations', version: [VERSION_NEUTRAL, '1'] })
export class AffiliationController {
constructor(
private _affiliationService: AffiliationService,
private _characterSheetRepository: CharacterSheetRepository,
) {}

@Get('/:id')
async find(@Param() param: QueryAffiliationDto): Promise<any> {
const result = await this._characterSheetRepository.findOne({
id: param.characterSheetId,
});

if (!result) {
return [];
}

return result.affiliation ?? [];
}

// :id/:id
// POST :id/:id +2 -2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import supertest from 'supertest';
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { FakerFactory } from '@cats-cradle/faker-factory';
import {
closeInMongodConnection,
rootMongooseTestModule,
MongooseModule,
} from '@cats-cradle/nestjs-modules';
import { AffiliationController } from './affiliation.controller';
import { AffiliationService } from './affiliation.service';
import { CharacterSheetRepository } from '../../models/character-sheet.repository';
import { CharacterSheet, CharacterSheetSchema } from '../../models/character-sheet.schema';

describe('/affiliations', () => {
let app: INestApplication;
let affiliationService: AffiliationService;
let characterSheetRepository: CharacterSheetRepository;

beforeEach(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [
rootMongooseTestModule(),
MongooseModule.forFeature([
{ name: 'CharacterSheet', schema: CharacterSheetSchema },
]),
],
providers: [CharacterSheetRepository, AffiliationService],
controllers: [AffiliationController],
}).compile();

app = moduleRef.createNestApplication();
affiliationService = moduleRef.get<AffiliationService>(AffiliationService);
characterSheetRepository = moduleRef.get<CharacterSheetRepository>(
CharacterSheetRepository,
);
await app.init();
});

afterEach(async () => {
await characterSheetRepository.deleteAll();
});

afterAll(async () => {
await closeInMongodConnection();
await app.close();
});

it('/GET /affiliations/:id', async () => {
const characterSheet = await FakerFactory.create<CharacterSheet>(
CharacterSheet,
{ archetypeId: 'MEEKU_ONI' },
);
await characterSheetRepository.create(characterSheet);

const response = await supertest(app.getHttpServer())
.get(`/affiliations/${characterSheet._id}`)
.expect(200);

expect(response.body).toEqual(characterSheet.affiliation);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { AffiliationService } from './affiliation.service';
import { AffiliationController } from './affiliation.controller';
import { CharacterSheetRepository } from '../../models/character-sheet.repository';

@Module({
controllers: [AffiliationController],
providers: [AffiliationService, CharacterSheetRepository],
})
export class AffiliationModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';

export enum Reputation {
HONORED = 'HONORED',
FRIENDLY = 'FRIENDLY',
NEUTRAL = 'NEUTRAL',
HATED = 'HATED',
}

@Injectable()
export class AffiliationService {
toString(amount: number | undefined): Reputation {
if (amount === undefined) {
return Reputation.NEUTRAL;
}
if (amount > 90) {
return Reputation.HONORED;
}
if (amount > 50) {
return Reputation.FRIENDLY;
}

if (amount < 0) {
return Reputation.HATED;
}

return Reputation.NEUTRAL;
}

async find(instanceId: string, characterId: string): Promise<any> {
// get from mongo
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { v4 } from 'uuid';

export class QueryAffiliationDto {
@ApiProperty({
default: v4(),
})
characterSheetId: string;
}

0 comments on commit b12a9af

Please sign in to comment.