Skip to content

Commit

Permalink
feat: block user from adding referral code if they have added for tha…
Browse files Browse the repository at this point in the history
…t service already
  • Loading branch information
samhwang committed Mar 7, 2024
1 parent 21322a3 commit 37a5464
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/commands/referral/referralNew.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ describe('execute', () => {
expect(mockChatInputInteraction.reply).toBeCalledWith('expiry_date has already expired');
});

it('should block adding referral code if the user already has one', async () => {
const data = {
service: services[0],
code: 'SomeCodeHere',
expiry_date: `04/04/${new Date().getFullYear() + 1}`,
userId: '1234',
};

const mockPrismaClient = mockDeep<PrismaClient>();
mockPrismaClient.referralCode.findFirst.mockResolvedValueOnce({
id: '12345',
service: data.service,
code: data.code,
expiry_date: new Date(data.expiry_date),
userId: data.userId,
});
mockGetDbClient.mockReturnValueOnce(mockPrismaClient);

mockChatInputInteraction.user.id = data.userId;
mockChatInputInteraction.options.getString.mockImplementation((name: string, required?: boolean) => {
if (name === 'service') return data.service;
if (name === 'link_or_code') return data.code;
if (name === 'expiry_date') return data.expiry_date;

return null;
});

await execute(mockChatInputInteraction);

expect(mockChatInputInteraction.reply).toBeCalledWith(`You have already entered the referral code for ${services[0]}.`);
});

it('should create a new referral code', async () => {
const data = {
service: services[0],
Expand Down
13 changes: 13 additions & 0 deletions src/commands/referral/referralNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export const execute: CommandHandler = async (interaction) => {
const userId = interaction.user.id;
const nickname = interaction.user.displayName;

const existingReferralCode = await db.referralCode.findFirst({
where: {
service,
userId,
},
});

if (existingReferralCode) {
logger.error(`[referral-new]: Referral code for ${service} by ${nickname} already exists.`);
await interaction.reply(`You have already entered the referral code for ${service}.`);
return;
}

try {
const newReferralCode = await db.referralCode.create({
data: {
Expand Down

0 comments on commit 37a5464

Please sign in to comment.