Skip to content

Commit

Permalink
feat(nita-repository): add countExistsNita
Browse files Browse the repository at this point in the history
Signed-off-by: Ren Adachi <[email protected]>
  • Loading branch information
moeyashi committed Mar 22, 2024
1 parent 03b9ea9 commit 85b1c1b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/infra/repository/nita.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,15 @@ export const postgresNitaRepository = () => {
const rank = Number(results[0].rank);
return rank || null;
},
async countExistsNita(trackCode, discordMembers) {
const discordUserIds = discordMembers.map((member) => member.user.id);
const results = await sql`
SELECT COUNT(*) as count
FROM nita
WHERE discord_user_id IN ${sql(discordUserIds)}
AND track_code = ${trackCode}
`;
return Number(results[0].count);
},
};
};
48 changes: 48 additions & 0 deletions src/infra/repository/nita.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,52 @@ describe('postgresNitaRepository', () => {
expect(actual).toBeNull();
});
});
describe('countExistsNita', async () => {
const trackCode = 'MKS';
beforeAll(async () => {
execSync('npm run db:reset');
});

test('0件の場合0を返却する', async () => {
const users = Array.from({ length: 21 }).map((_, i) => ({
user: { id: `${i + 1}` },
}));
const actual = await repo.countExistsNita(trackCode, users);
expect(actual).toEqual(0);
});

test('1件の場合1を返却する', async () => {
const users = Array.from({ length: 21 }).map((_, i) => ({
user: { id: `${i + 1}` },
}));
await repo.insertNita({
discordUserId: '1',
trackCode,
milliseconds: 1000,
});
const actual = await repo.countExistsNita(trackCode, users);
expect(actual).toEqual(1);
await repo.deleteNita('1', trackCode);
});

test('2件の場合2を返却する', async () => {
const users = Array.from({ length: 21 }).map((_, i) => ({
user: { id: `${i + 1}` },
}));
await repo.insertNita({
discordUserId: '1',
trackCode,
milliseconds: 1000,
});
await repo.insertNita({
discordUserId: '2',
trackCode,
milliseconds: 1000,
});
const actual = await repo.countExistsNita(trackCode, users);
expect(actual).toEqual(2);
await repo.deleteNita('1', trackCode);
await repo.deleteNita('2', trackCode);
});
});
});
4 changes: 4 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export type NitaRepository = {
discordUserId: string,
discordMembers: GuildMember[]
) => Promise<number | null>;
countExistsNita: (
trackCode: string,
discordMembers: GuildMember[]
) => Promise<number>;
};

// 参考 https://typescriptbook.jp/reference/functions/overload-functions#%E3%82%A2%E3%83%AD%E3%83%BC%E9%96%A2%E6%95%B0%E3%81%A8%E3%82%AA%E3%83%BC%E3%83%90%E3%83%BC%E3%83%AD%E3%83%BC%E3%83%89
Expand Down

0 comments on commit 85b1c1b

Please sign in to comment.