Append timestamp to reviews

This commit is contained in:
powermaker450 2024-08-27 13:42:00 -04:00
parent a720a9a0a2
commit 92cbe519b7
3 changed files with 10 additions and 5 deletions

View file

@ -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>;

View file

@ -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);

View file

@ -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
};
}