Append timestamp to reviews
This commit is contained in:
parent
a720a9a0a2
commit
92cbe519b7
|
@ -22,6 +22,7 @@ export const userReviewSchema = object({
|
|||
|
||||
export const serverReviewSchema = userReviewSchema.shape({
|
||||
id: string().length(6).required(),
|
||||
timestamp: string().required()
|
||||
});
|
||||
|
||||
export type UserSideReview = InferType<typeof userReviewSchema>;
|
||||
|
|
|
@ -5,7 +5,7 @@ import {
|
|||
userRating,
|
||||
UserSideReview,
|
||||
} from "../types";
|
||||
import { appendId, checkFile, stripId } from "./functions";
|
||||
import { toServerReview, checkFile, stripId } from "./functions";
|
||||
|
||||
export class ReviewData {
|
||||
public data: ServerSideReview[];
|
||||
|
@ -16,7 +16,7 @@ export class ReviewData {
|
|||
|
||||
public async appendReview(review: UserSideReview): Promise<void> {
|
||||
await serverReviewSchema
|
||||
.validate(appendId(review))
|
||||
.validate(toServerReview(review))
|
||||
|
||||
.then((validReview) => {
|
||||
this.data.push(validReview);
|
||||
|
|
|
@ -26,21 +26,25 @@ export function checkFile(
|
|||
// Generates a unique 6-character ID
|
||||
export const generateId = (): string => crypto.randomBytes(3).toString("hex");
|
||||
|
||||
export function appendId(userReview: UserSideReview): ServerSideReview {
|
||||
export const getTimestamp = (): string => new Date().toISOString();
|
||||
|
||||
export function toServerReview(userReview: UserSideReview): ServerSideReview {
|
||||
return {
|
||||
rating: userReview.rating,
|
||||
username: userReview.username,
|
||||
title: userReview.title,
|
||||
content: userReview.content,
|
||||
id: generateId(),
|
||||
timestamp: getTimestamp()
|
||||
};
|
||||
}
|
||||
|
||||
export function stripId(serverReview: ServerSideReview): UserSideReview {
|
||||
export function stripId(serverReview: ServerSideReview) {
|
||||
return {
|
||||
rating: serverReview.rating,
|
||||
username: serverReview.username,
|
||||
title: serverReview.title,
|
||||
content: serverReview.content
|
||||
content: serverReview.content,
|
||||
timestamp: serverReview.timestamp
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue