From e49f290c024626ae698f02faa4c33a2afdb424e7 Mon Sep 17 00:00:00 2001 From: Veirt Date: Mon, 2 Dec 2024 17:07:12 +0800 Subject: [PATCH] feat(seeder): seed reviews --- src/module/order/order.repository.ts | 2 +- src/module/review/review.seeder.ts | 22 ++++++++++++++++++++++ src/seed.ts | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/module/review/review.seeder.ts diff --git a/src/module/order/order.repository.ts b/src/module/order/order.repository.ts index ce02466..2a23188 100644 --- a/src/module/order/order.repository.ts +++ b/src/module/order/order.repository.ts @@ -106,7 +106,7 @@ export class OrderRepository { async updateStatusToCompleted() { await this.db .update(orders) - .set({ status: "scheduled" }) + .set({ status: "completed" }) .where(lte(orders.estimatedEndTime, new Date())); } } diff --git a/src/module/review/review.seeder.ts b/src/module/review/review.seeder.ts new file mode 100644 index 0000000..3b5cd55 --- /dev/null +++ b/src/module/review/review.seeder.ts @@ -0,0 +1,22 @@ +import { container } from "@/container"; +import { faker } from "@faker-js/faker"; + +const orderRepository = container.orderRepository; +const reviewRepository = container.reviewRepository; + +export const seedReviews = async () => { + const orders = await orderRepository.getOrders({}); + + if (orders.length === 0) { + throw new Error("Order not found"); + } + + console.log(`Seeding reviews with ${orders.length} data...`); + for (let i = 0; i < orders.length; i++) { + await reviewRepository.createReview({ + orderId: orders[i].id, + rating: faker.number.int({ min: 1, max: 5 }), + message: faker.lorem.sentence(), + }); + } +}; diff --git a/src/seed.ts b/src/seed.ts index 827b51b..e4879ee 100644 --- a/src/seed.ts +++ b/src/seed.ts @@ -7,6 +7,7 @@ import { } from "@/module/tutor/tutor.seeder"; import { seedTutories } from "@/module/tutories/tutories.seeder"; import { seedOrders } from "./module/order/order.seeder"; +import { seedReviews } from "./module/review/review.seeder"; export const runSeeder = async () => { await seedCategories(); @@ -15,6 +16,7 @@ export const runSeeder = async () => { await seedTutories({ generateWithGroq: true }); await assignTutorProfilePictures(); await seedOrders(); + await seedReviews(); }; runSeeder();