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({ export const serverReviewSchema = userReviewSchema.shape({
id: string().length(6).required(), id: string().length(6).required(),
timestamp: string().required()
}); });
export type UserSideReview = InferType<typeof userReviewSchema>; export type UserSideReview = InferType<typeof userReviewSchema>;

View file

@ -5,7 +5,7 @@ import {
userRating, userRating,
UserSideReview, UserSideReview,
} from "../types"; } from "../types";
import { appendId, checkFile, stripId } from "./functions"; import { toServerReview, checkFile, stripId } from "./functions";
export class ReviewData { export class ReviewData {
public data: ServerSideReview[]; public data: ServerSideReview[];
@ -16,7 +16,7 @@ export class ReviewData {
public async appendReview(review: UserSideReview): Promise<void> { public async appendReview(review: UserSideReview): Promise<void> {
await serverReviewSchema await serverReviewSchema
.validate(appendId(review)) .validate(toServerReview(review))
.then((validReview) => { .then((validReview) => {
this.data.push(validReview); this.data.push(validReview);

View file

@ -26,21 +26,25 @@ export function checkFile(
// Generates a unique 6-character ID // Generates a unique 6-character ID
export const generateId = (): string => crypto.randomBytes(3).toString("hex"); 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 { return {
rating: userReview.rating, rating: userReview.rating,
username: userReview.username, username: userReview.username,
title: userReview.title, title: userReview.title,
content: userReview.content, content: userReview.content,
id: generateId(), id: generateId(),
timestamp: getTimestamp()
}; };
} }
export function stripId(serverReview: ServerSideReview): UserSideReview { export function stripId(serverReview: ServerSideReview) {
return { return {
rating: serverReview.rating, rating: serverReview.rating,
username: serverReview.username, username: serverReview.username,
title: serverReview.title, title: serverReview.title,
content: serverReview.content content: serverReview.content,
timestamp: serverReview.timestamp
}; };
} }