Skip to content

Commit

Permalink
refactor: CreateTeamServiceに変更、古い定義に基づくハードコーディングを削除
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Sep 7, 2024
1 parent 4baf8a6 commit 97fa257
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/kcms/src/team/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TeamRepository } from './models/repository.js';
import { CreateTeamService } from './service/entry.js';
import { CreateTeamService } from './service/createTeam';
import { Result, Option } from '@mikuroxina/mini-fn';
import { FindEntryService } from './service/get.js';
import { DeleteEntryService } from './service/delete.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest';
import { DummyRepository } from '../adaptor/dummyRepository.js';
import { Team, TeamID } from '../models/team.js';
import { Result } from '@mikuroxina/mini-fn';
import { CreateTeamService } from './entry.js';
import { CreateTeamService } from './createTeam';
import { TestEntryData } from '../../testData/entry.js';
import { SnowflakeIDGenerator } from '../../id/main.js';

Expand All @@ -17,7 +17,7 @@ describe('entryService', () => {
repository.reset();
});

it('エントリーできる', async () => {
it('参加登録できる', async () => {
const data = TestEntryData['ElementaryMultiWalk'];
const actual = await service.create({
teamName: data.getTeamName(),
Expand Down Expand Up @@ -57,60 +57,41 @@ describe('entryService', () => {
expect(result[1]).toStrictEqual(new Error('teamName Exists'));
});

it('オープン部門のメンバーは1人のみ', async () => {
const entry = Team.new({
id: '123' as TeamID,
teamName: 'team1',
members: ['山田四十郎', '山田太郎'],
isMultiWalk: true,
category: 'Open',
});
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('too many members'));
});

it('小学生部門のメンバーは1または2人', async () => {
const entry = Team.new({
it('メンバーが居ないチームは作れない', async () => {
const team = Team.new({
id: '123' as TeamID,
teamName: 'team1',
members: ['山田四十郎', '山田太郎', '山田次郎'],
members: [],
isMultiWalk: true,
category: 'Elementary',
});
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
teamName: team.getTeamName(),
members: team.getMembers(),
isMultiWalk: team.getIsMultiWalk(),
category: team.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('too many members'));
expect(actual[1]).toStrictEqual(new Error('no member'));
});

it('メンバーが居ないチームは作れない', async () => {
const entry = Team.new({
it('メンバーが3人以上のチームは作れない', async () => {
const team = Team.new({
id: '123' as TeamID,
teamName: 'team1',
members: [],
members: ['A太郎', 'B太郎', 'C太郎'],
isMultiWalk: true,
category: 'Elementary',
});
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
teamName: team.getTeamName(),
members: team.getMembers(),
isMultiWalk: team.getIsMultiWalk(),
category: team.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('no member'));
expect(actual[1]).toStrictEqual(new Error('too many members'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ export class CreateTeamService {
this.idGenerator = idGenerator;
}

/**
* チームを作成(参加登録)します\
* - チーム名は重複できない
* - チームのメンバー数は最大2人
* @param input {@link TeamCreateArgs} チームのデータ
*/
async create(
input: Omit<TeamCreateArgs, 'id' | 'isEntered'>
): Promise<Result.Result<Error, Team>> {
const id = this.idGenerator.generate<Team>();
if (Result.isErr(id)) {
return Result.err(id[1]);
}
const createArgs: Omit<TeamCreateArgs, 'isEntered'> = {
id: id[1],
teamName: input.teamName,
members: input.members,
isMultiWalk: input.isMultiWalk,
category: input.category,
};

// チーム名は重複できない
if (await this.isExists(input.teamName)) {
Expand All @@ -38,18 +37,18 @@ export class CreateTeamService {
return Result.err(new Error('no member'));
}

if (input.category === 'Open') {
// オープン部門->1人
if (input.members.length > 1) {
return Result.err(new Error('too many members'));
}
} else {
// 小学生部門 -> 1 or 2人
if (input.members.length > 2) {
return Result.err(new Error('too many members'));
}
// 最大は2人
if (input.members.length > 2) {
return Result.err(new Error('too many members'));
}

const createArgs: Omit<TeamCreateArgs, 'isEntered'> = {
id: id[1],
teamName: input.teamName,
members: input.members,
isMultiWalk: input.isMultiWalk,
category: input.category,
};
const e = Team.new(createArgs);
const res = await this.repository.create(e);
if (Result.isErr(res)) {
Expand All @@ -59,6 +58,9 @@ export class CreateTeamService {
return Result.ok(res[1]);
}

/**
* チーム名が存在するかを返す
*/
private async isExists(teamName: string) {
const res = await this.repository.findByTeamName(teamName);
return Option.isSome(res);
Expand Down

0 comments on commit 97fa257

Please sign in to comment.