Skip to content

Commit

Permalink
added ALREADY_APPLIED error code
Browse files Browse the repository at this point in the history
This commit introduces a new error code, ALREADY_APPLIED (along with the related test cases) to provide a more accurate error message for users who attempt to apply a partner access code that has already been used with their account.
  • Loading branch information
mayank-sahai committed Apr 25, 2024
1 parent cf576d4 commit f3c47bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/partner-access/partner-access.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { Repository } from 'typeorm';
import { createQueryBuilderMock } from '../../test/utils/mockUtils';
import { PartnerAccessEntity } from '../entities/partner-access.entity';
import { PartnerAccessCodeStatusEnum } from '../utils/constants';
import { CreatePartnerAccessDto } from './dtos/create-partner-access.dto';
import { GetPartnerAccessesDto } from './dtos/get-partner-access.dto';
import { UpdatePartnerAccessDto } from './dtos/update-partner-access.dto';
Expand Down Expand Up @@ -165,6 +166,30 @@ describe('PartnerAccessService', () => {
activatedAt: partnerAccess.activatedAt, // need to just fudge this as it is test specific
});
});
it('should return an error when partner access code has already been used by another user account', async () => {
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'createQueryBuilder');
repoSpyCreateQueryBuilder.mockImplementation(
createQueryBuilderMock({
getOne: jest.fn().mockResolvedValue({ id: '123456', userId: 'anotherUserId' }),
}) as never,
);

await expect(service.assignPartnerAccess(mockGetUserDto, '123456')).rejects.toThrow(
PartnerAccessCodeStatusEnum.ALREADY_IN_USE,
);
});
it('should return an error when partner access code has already been applied to the account', async () => {
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'createQueryBuilder');
repoSpyCreateQueryBuilder.mockImplementation(
createQueryBuilderMock({
getOne: jest.fn().mockResolvedValue({ id: '123456', userId: mockGetUserDto.user.id }),
}) as never,
);

await expect(service.assignPartnerAccess(mockGetUserDto, '123456')).rejects.toThrow(
PartnerAccessCodeStatusEnum.ALREADY_APPLIED,
);
});
});

describe('assignPartnerAccessOnSignUp', () => {
Expand Down
13 changes: 10 additions & 3 deletions src/partner-access/partner-access.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export class PartnerAccessService {
.getOne();
}

async getValidPartnerAccessCode(partnerAccessCode: string): Promise<PartnerAccessEntity> {
async getValidPartnerAccessCode(
partnerAccessCode: string,
userId?: string,
): Promise<PartnerAccessEntity> {
const format = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/;

if (format.test(partnerAccessCode) || partnerAccessCode.length !== 6) {
Expand All @@ -77,7 +80,11 @@ export class PartnerAccessService {
}

if (partnerAccess.userId) {
throw new HttpException(PartnerAccessCodeStatusEnum.ALREADY_IN_USE, HttpStatus.CONFLICT);
if (userId && partnerAccess.userId === userId) {
throw new HttpException(PartnerAccessCodeStatusEnum.ALREADY_APPLIED, HttpStatus.CONFLICT);
} else {
throw new HttpException(PartnerAccessCodeStatusEnum.ALREADY_IN_USE, HttpStatus.CONFLICT);
}
}

// ensure the partner access code has been created no more than a year ago
Expand Down Expand Up @@ -180,7 +187,7 @@ export class PartnerAccessService {
{ user, partnerAccesses, courses }: GetUserDto,
partnerAccessCode: string,
): Promise<PartnerAccessEntity> {
const partnerAccess = await this.getValidPartnerAccessCode(partnerAccessCode);
const partnerAccess = await this.getValidPartnerAccessCode(partnerAccessCode, user.id);

partnerAccess.userId = user.id;
partnerAccess.activatedAt = new Date();
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export enum PartnerAccessCodeStatusEnum {
INVALID_CODE = 'INVALID_CODE',
DOES_NOT_EXIST = 'DOES_NOT_EXIST',
ALREADY_IN_USE = 'ALREADY_IN_USE',
ALREADY_APPLIED = 'ALREADY_APPLIED',
CODE_EXPIRED = 'CODE_EXPIRED',
}

Expand Down

0 comments on commit f3c47bc

Please sign in to comment.