Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle inavalid receipt #32

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions backend/src/review/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ export class ReviewService {
restaurant: restaurantDto,
} = createReviewDto;

console.log(createReviewDto);

const restaurant =
await this.restaurantRepository.findOrCreate(restaurantDto);
const images = await this.imageRepository.findBy({
let images = await this.imageRepository.findBy({
id: In(imageIds.concat([receiptImageId ?? -1])),
});
const receiptImage = images.find((image) => image.id === receiptImageId);
Expand All @@ -37,11 +35,10 @@ export class ReviewService {
await receiptImage.markAsReceipt();
try {
const receiptData = await getReceiptOcr(receiptImage.url);
console.log(receiptData);
await receiptImage.markAsReceiptVerified();
menu = receiptData['menu'];
} catch (e) {
throw new BadRequestException('잘못된 영수증입니다.');
images = images.filter((image) => image.id !== receiptImageId);
}
}

Expand Down
23 changes: 20 additions & 3 deletions backend/src/test/review/create-review.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ describe('Create Review test', () => {
.expect(HttpStatus.BAD_REQUEST);
});

it('영수증 에러나면 400', async () => {
it('영수증 에러나면 사진 빠진다', async () => {
const image = await ImageFixture.create({});

const receiptImageId = await ImageFixture.create({});

jest.clearAllMocks();
(getReviewIsPositive as jest.Mock).mockRejectedValue(new Error('error'));
(getReceiptOcr as jest.Mock).mockRejectedValue(new Error('error'));

await supertest(testServer.getHttpServer())
.post('/reviews')
Expand All @@ -208,7 +208,24 @@ describe('Create Review test', () => {
receiptImageId: receiptImageId.id,
})
.set('Authorization', `Bearer ${accessToken}`)
.expect(HttpStatus.BAD_REQUEST);
.expect(HttpStatus.CREATED);

const review = await ReviewEntity.findOneOrFail({
where: {
content: 'content',
},
relations: {
restaurant: true,
images: true,
user: true,
},
});

expect(review.images.length).toEqual(1);
expect(review.images[0].id).toEqual(image.id);
expect(review.images[0].isReceipt).toEqual(false);
expect(review.images[0].isReceiptVerified).toEqual(false);
expect(review.images[0].id).not.toEqual(receiptImageId.id);
});

it('레스토랑 없었으면 생성한다.', async () => {
Expand Down
Loading