Skip to content

Commit

Permalink
feat: Show display name of who added the referral code when user gets…
Browse files Browse the repository at this point in the history
… a random one
  • Loading branch information
samhwang committed Mar 7, 2024
1 parent 37a5464 commit 4aa10cf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
39 changes: 36 additions & 3 deletions src/commands/referral/referralRandom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { AutocompleteInteraction, ChatInputCommandInteraction } from 'discord.js';
import { AutocompleteInteraction, ChatInputCommandInteraction, Collection, GuildMember } from 'discord.js';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { captor, mockDeep, mockReset } from 'vitest-mock-extended';
import { getDbClient } from '../../clients';
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('execute', () => {
expect(mockPrismaClient.referralCode.findMany).toBeCalledWith(mockReferralInput);
expect(mockReferralInput.value.where.service.contains).toEqual(service);
expect(mockChatInputInteraction.reply).toBeCalledWith(replyInput);
expect(replyInput.value).toEqual(`There is no code for ${service.trim().toLowerCase()} service`);
expect(replyInput.value).toContain(`There is no code for ${service} service`);
});

it('should return code if found', async () => {
Expand All @@ -105,6 +105,7 @@ describe('execute', () => {
code,
expiry_date: expiryDate,
service,
userId: '1234',
},
]);
mockGetDbClient.mockReturnValueOnce(mockPrismaClient);
Expand All @@ -116,6 +117,38 @@ describe('execute', () => {
expect(mockPrismaClient.referralCode.findMany).toBeCalledWith(mockReferralInput);
expect(mockReferralInput.value.where.service.contains).toEqual(service);
expect(mockChatInputInteraction.reply).toBeCalledWith(replyInput);
expect(replyInput.value).toEqual(`Service ${service.trim().toLowerCase()}: ${code}`);
expect(replyInput.value).toContain(`Service ${service}: ${code} added by user 1234`);
});

it('should return member name if found', async () => {
const service = 'some service';
const code = 'SomeCode';
const expiryDate = new Date(`05/04/${new Date().getFullYear() + 1}`);

const mockReferralInput = captor<{
where: { service: { contains: string } };
}>();
mockPrismaClient.referralCode.findMany.mockResolvedValueOnce([
{
id: '1',
code,
expiry_date: expiryDate,
service,
userId: '1234',
},
]);
mockGetDbClient.mockReturnValueOnce(mockPrismaClient);
mockChatInputInteraction.options.getString.mockReturnValueOnce(service);
const members = new Collection<string, GuildMember>();
members.set('1234', { displayName: 'SomeMember' } as GuildMember);
mockChatInputInteraction.guild?.members.fetch.mockResolvedValueOnce(members);
const replyInput = captor<string>();

await execute(mockChatInputInteraction);

expect(mockPrismaClient.referralCode.findMany).toBeCalledWith(mockReferralInput);
expect(mockReferralInput.value.where.service.contains).toEqual(service);
expect(mockChatInputInteraction.reply).toBeCalledWith(replyInput);
expect(replyInput.value).toContain(`Service ${service.trim().toLowerCase()}: ${code}`);
});
});
17 changes: 14 additions & 3 deletions src/commands/referral/referralRandom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SlashCommandSubcommandBuilder } from 'discord.js';
import { getDbClient } from '../../clients';
import { getRandomIntInclusive } from '../../utils';
import { logger } from '../../utils/logger';
import { AutocompleteHandler, CommandHandler } from '../builder';
import { searchServices } from './services';

Expand All @@ -19,7 +20,7 @@ export const autocomplete: AutocompleteHandler = async (interaction) => {
};

export const execute: CommandHandler = async (interaction) => {
const service = interaction.options.getString('service', true)?.trim().toLowerCase() ?? '';
const service = interaction.options.getString('service', true).trim().toLowerCase();

const db = getDbClient();
const referrals = await db.referralCode.findMany({
Expand All @@ -34,10 +35,20 @@ export const execute: CommandHandler = async (interaction) => {
});

if (referrals.length === 0) {
await interaction.reply(`There is no code for ${service} service`);
logger.info(`[referral-random]: There is no code for ${service} service in the system.`);
await interaction.reply(`There is no code for ${service} service in the system.`);
return;
}

const referral = referrals[getRandomIntInclusive(0, referrals.length - 1)];
await interaction.reply(`Service ${service}: ${referral.code}`);
logger.info(`[referral-random]: Found referral code for ${service}, code: ${referral.code}, by user ${referral.userId}`);

let displayName = referral.userId;
const member = await interaction.guild?.members.fetch(referral.userId);
if (member) {
logger.info(`[referral-random]: Found member ${member.displayName} from userId ${referral.userId}`);
displayName = member.displayName;
}

await interaction.reply(`Service ${service}: ${referral.code} added by user ${displayName}`);
};

0 comments on commit 4aa10cf

Please sign in to comment.