// Check that the user hasn't been referred before
const referral = await this.prisma.referralCode.findMany({
where: {
user_id: userId,
type: ReferralType.USER,
},
});
if (referral?.length > 0) {
throw new BadRequestException(USER_ALREADY_REFERRED);
}
// If the code exists, check its validity
const referralCode = await this.prisma.referralCode.findUnique({
where: {
code: referral_code,
},
});
if (!referralCode) {
throw new BadRequestException(INVALID_REFERRAL_CODE);
}
// DB writes after this (redacted)
}
I am trying to write a test which would test addReferral thoroughly
So you could abstract these things, but really there are already problems here I think
I would probably go even more nuts with errors: class UserAlreadyReferredError extends Error {} class InvalidReferralCodeError extends Error {}
Обсуждают сегодня