diff --git a/src/commands/referral/referralRandom.test.ts b/src/commands/referral/referralRandom.test.ts index e3b7f6a4..44baa443 100644 --- a/src/commands/referral/referralRandom.test.ts +++ b/src/commands/referral/referralRandom.test.ts @@ -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'; @@ -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 () => { @@ -105,6 +105,7 @@ describe('execute', () => { code, expiry_date: expiryDate, service, + userId: '1234', }, ]); mockGetDbClient.mockReturnValueOnce(mockPrismaClient); @@ -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(); + members.set('1234', { displayName: 'SomeMember' } as GuildMember); + mockChatInputInteraction.guild?.members.fetch.mockResolvedValueOnce(members); + const replyInput = captor(); + + 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}`); }); }); diff --git a/src/commands/referral/referralRandom.ts b/src/commands/referral/referralRandom.ts index 613f3f64..511e50b2 100644 --- a/src/commands/referral/referralRandom.ts +++ b/src/commands/referral/referralRandom.ts @@ -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'; @@ -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({ @@ -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}`); };