This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from SvelteShipSolutions/fix/review
Fix: Review of Delivery
- Loading branch information
Showing
15 changed files
with
1,316 additions
and
730 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...migrations/20241125052739_remove_img_review_and_update_shipment_transaction/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- CreateTable | ||
CREATE TABLE "Review" ( | ||
"id" SERIAL NOT NULL, | ||
"rating" INTEGER NOT NULL, | ||
"deliveryRating" INTEGER NOT NULL, | ||
"wasDeliveryOnTime" BOOLEAN NOT NULL, | ||
"comment" TEXT, | ||
"userId" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"transactionId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "Review_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Review_transactionId_key" ON "Review"("transactionId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Review" ADD CONSTRAINT "Review_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Review" ADD CONSTRAINT "Review_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES "ShipmentTransaction"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
model Review { | ||
id Int @id @default(autoincrement()) | ||
user User @relation(fields: [userId], references: [id]) | ||
rating Int | ||
deliveryRating Int | ||
wasDeliveryOnTime Boolean | ||
comment String? | ||
userId Int | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
images Image[] | ||
} | ||
|
||
model Image { | ||
id Int @id @default(autoincrement()) | ||
url String | ||
reviewId Int | ||
review Review @relation(fields: [reviewId], references: [id]) | ||
id Int @id @default(autoincrement()) | ||
user User @relation(fields: [userId], references: [id]) | ||
rating Int | ||
deliveryRating Int | ||
wasDeliveryOnTime Boolean | ||
comment String? | ||
userId Int | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
shipmentTransaction ShipmentTransaction @relation(fields: [transactionId], references: [id]) | ||
transactionId Int @unique | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ model ShipmentTransaction { | |
shipperId Int | ||
trackingEvents TrackingEvent[] | ||
Review Review? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export class Review { | ||
id?: number; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
userId: number; | ||
rating: number; | ||
deliveryRating: number; | ||
wasDeliveryOnTime: boolean; | ||
comment?: string; | ||
transactionId: number; | ||
|
||
constructor(params: { | ||
id?: number; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
userId: number; | ||
rating: number; | ||
deliveryRating: number; | ||
wasDeliveryOnTime: boolean; | ||
comment?: string | null; | ||
transactionId: number; | ||
}) { | ||
this.id = params.id; | ||
this.createdAt = params.createdAt; | ||
this.updatedAt = params.updatedAt; | ||
this.userId = params.userId; | ||
this.rating = params.rating; | ||
this.deliveryRating = params.deliveryRating; | ||
this.wasDeliveryOnTime = params.wasDeliveryOnTime; | ||
this.comment = params.comment ?? undefined; | ||
this.transactionId = params.transactionId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Review } from "./Review"; | ||
|
||
export class ReviewFactory { | ||
static create(params: { | ||
userId: number; | ||
rating: number; | ||
deliveryRating: number; | ||
wasDeliveryOnTime: boolean; | ||
comment?: string; | ||
transactionId: number; | ||
}): Review { | ||
return new Review({ | ||
userId: params.userId, | ||
rating: params.rating, | ||
deliveryRating: params.deliveryRating, | ||
wasDeliveryOnTime: params.wasDeliveryOnTime, | ||
comment: params.comment ?? undefined, | ||
transactionId: params.transactionId, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { prisma } from "$lib/db/client"; | ||
import { Review } from "./Review"; | ||
|
||
export class ReviewRepository { | ||
static async save(review: Review): Promise<Review> { | ||
const dataFields = { | ||
userId: review.userId, | ||
rating: review.rating, | ||
deliveryRating: review.deliveryRating, | ||
wasDeliveryOnTime: review.wasDeliveryOnTime, | ||
comment: review.comment ?? undefined, // Normalize null to undefined | ||
transactionId: review.transactionId, | ||
}; | ||
|
||
const savedReview = await prisma.review.upsert({ | ||
where: { transactionId: review.transactionId }, | ||
update: dataFields, | ||
create: dataFields, | ||
}); | ||
|
||
review.id = savedReview.id; | ||
review.createdAt = savedReview.createdAt; | ||
review.updatedAt = savedReview.updatedAt; | ||
|
||
return review; | ||
} | ||
|
||
static async findById(id: number): Promise<Review | null> { | ||
const dbResult = await prisma.review.findUnique({ | ||
where: { id }, | ||
}); | ||
|
||
if (dbResult) { | ||
return new Review({ | ||
id: dbResult.id, | ||
createdAt: dbResult.createdAt, | ||
updatedAt: dbResult.updatedAt, | ||
userId: dbResult.userId, | ||
rating: dbResult.rating, | ||
deliveryRating: dbResult.deliveryRating, | ||
wasDeliveryOnTime: dbResult.wasDeliveryOnTime, | ||
comment: dbResult.comment, | ||
transactionId: dbResult.transactionId, | ||
}); | ||
} | ||
return null; | ||
} | ||
|
||
static async findByTransactionId( | ||
transactionId: number, | ||
): Promise<Review | null> { | ||
const dbResult = await prisma.review.findUnique({ | ||
where: { transactionId }, | ||
}); | ||
|
||
if (dbResult) { | ||
return new Review({ | ||
id: dbResult.id, | ||
createdAt: dbResult.createdAt, | ||
updatedAt: dbResult.updatedAt, | ||
userId: dbResult.userId, | ||
rating: dbResult.rating, | ||
deliveryRating: dbResult.deliveryRating, | ||
wasDeliveryOnTime: dbResult.wasDeliveryOnTime, | ||
comment: dbResult.comment, | ||
transactionId: dbResult.transactionId, | ||
}); | ||
} | ||
return null; | ||
} | ||
|
||
static async findByUserId(userId: number): Promise<Review[]> { | ||
const dbResults = await prisma.review.findMany({ | ||
where: { userId }, | ||
}); | ||
|
||
return dbResults.map( | ||
(dbResult) => | ||
new Review({ | ||
id: dbResult.id, | ||
createdAt: dbResult.createdAt, | ||
updatedAt: dbResult.updatedAt, | ||
userId: dbResult.userId, | ||
rating: dbResult.rating, | ||
deliveryRating: dbResult.deliveryRating, | ||
wasDeliveryOnTime: dbResult.wasDeliveryOnTime, | ||
comment: dbResult.comment, | ||
transactionId: dbResult.transactionId, | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.