Skip to content

Commit

Permalink
feat: implement update reply repository method
Browse files Browse the repository at this point in the history
  • Loading branch information
skgndi12 committed Mar 11, 2024
1 parent 8873aa4 commit 394fb0b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
CreateReplyParams,
FindManyAndCountResponse,
FindRepliesParams,
ReplyRepository
ReplyRepository,
UpdateReplyParams
} from '@src/core/ports/reply.repository';
import { AppErrorCode, CustomError } from '@src/error/errors';
import {
Expand Down Expand Up @@ -114,4 +115,40 @@ export class PostgresqlReplyRepository implements Partial<ReplyRepository> {
});
}
};

public update = async (
params: UpdateReplyParams,
txClient?: ExtendedPrismaTransactionClient
): Promise<Reply> => {
const updatedAt = new Date();
try {
const client = txClient ?? this.client;
const replyUpdated = await client.reply.update({
where: { id: params.id },
data: {
content: params.content,
updatedAt
}
});

return replyUpdated.convertToEntity();
} catch (error: unknown) {
if (
isErrorWithCode(error) &&
error.code === PrismaErrorCode.RECORD_NOT_FOUND
) {
throw new CustomError({
code: AppErrorCode.NOT_FOUND,
message: 'reply not found for deletion',
context: { params }
});
}
throw new CustomError({
code: AppErrorCode.INTERNAL_ERROR,
cause: error,
message: 'failed to update reply',
context: { params }
});
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
} from '@src/core/nickname.generator';
import {
CreateReplyParams,
FindRepliesParams
FindRepliesParams,
UpdateReplyParams
} from '@src/core/ports/reply.repository';
import { AccessLevelEnum, IdpEnum } from '@src/core/types';
import { AppErrorCode, CustomError } from '@src/error/errors';
Expand Down Expand Up @@ -326,4 +327,96 @@ describe('Test reply repository', () => {
expect(JSON.stringify(actualResult.replies)).toEqual(JSON.stringify([]));
});
});

describe('Test update', () => {
const userId = randomUUID();
const reviewId = generateRandomNumber(userId);
const content = 'randomContent';
const createdAt = new Date();
const contentUpdated = 'updatedContent';
const updatedAt = new Date();
let replyCreated: Reply;

beforeAll(async () => {
await prismaClient.user.create({
data: {
id: userId,
nickname: generateUserNickname(userId),
tag: generateUserTag(userId),
idp: Idp.GOOGLE,
email: `${userId}@gmail.com`,
accessLevel: AccessLevel.USER,
createdAt,
updatedAt: createdAt
}
});
await prismaClient.review.create({
data: {
id: reviewId,
userId,
title: 'randomTitle',
movieName: 'randomMovieName',
content,
createdAt,
updatedAt: createdAt
}
});
const replyResultCreated = await prismaClient.reply.create({
data: {
reviewId,
userId,
content,
createdAt,
updatedAt: createdAt
}
});
replyCreated = replyResultCreated.convertToEntity();
});

afterAll(async () => {
await prismaClient.review.delete({ where: { id: reviewId } });
await prismaClient.user.delete({ where: { id: userId } });
});

it('should success when valid', async () => {
const replyExpectResult = new Reply(
replyCreated.id,
replyCreated.reviewId,
replyCreated.userId,
contentUpdated,
replyCreated.createdAt,
updatedAt
);
const params: UpdateReplyParams = {
id: replyCreated.id,
content: contentUpdated
};

const replyUpdated = await replyRepository.update(params);

expect(replyUpdated.getData()).toEqual(
expect.objectContaining({
id: replyExpectResult.id,
reviewId: replyExpectResult.reviewId,
userId: replyExpectResult.userId,
content: replyExpectResult.content,
createdAt: replyExpectResult.createdAt
})
);
});

it('should fail to update a reply when no existing reply is found with the given ID', async () => {
const params: UpdateReplyParams = {
id: replyCreated.id,
content: contentUpdated
};

try {
await replyRepository.update(params);
} catch (error: unknown) {
expect(error).toBeInstanceOf(CustomError);
expect(error).toHaveProperty('code', AppErrorCode.NOT_FOUND);
}
});
});
});

0 comments on commit 394fb0b

Please sign in to comment.