65 lines
2 KiB
TypeScript
65 lines
2 KiB
TypeScript
import { assertUnreachable } from '@app/client/helpers';
|
|
import { call } from '@app/common';
|
|
import { prisma } from '@app/server/prisma/client';
|
|
import {
|
|
WorkingProgressStatus as WorkingProgressStatusSchema,
|
|
Working as WorkingSchema,
|
|
WorkingType as WorkingTypeSchema
|
|
} from '@generated/types';
|
|
import { Working, WorkingProgressStatus, WorkingType } from '@prisma/client';
|
|
import { toUserSchema } from './userMapper';
|
|
|
|
export async function toWorkingSchema(model: Working): Promise<WorkingSchema> {
|
|
return {
|
|
id: model.id,
|
|
title: model.title,
|
|
description: model.description,
|
|
author: await call(async () => {
|
|
const userModel = await prisma.user.findUniqueOrThrow({ where: { id: model.authorId } });
|
|
return toUserSchema(userModel);
|
|
}),
|
|
createdAt: model.createdAt.toISOString(),
|
|
updatedAt: model.updatedAt.toISOString(),
|
|
type: call(() => {
|
|
ensureWorkingType(model.type);
|
|
return model.type;
|
|
}),
|
|
progressStatus: call(() => {
|
|
ensureWorkingProgressStatus(model.progressStatus);
|
|
return model.progressStatus;
|
|
}),
|
|
//todo: ratings implementation
|
|
//todo: book/notes implementation
|
|
ratingTotal: await call(async () => {
|
|
const ratingsCount = await prisma.rating.count({
|
|
where: {
|
|
workingId: model.id
|
|
}
|
|
});
|
|
return ratingsCount;
|
|
}),
|
|
reviewsTotal: await call(async () => {
|
|
const reviewsCount = await prisma.review.count({
|
|
where: {
|
|
workingId: model.id
|
|
}
|
|
});
|
|
return reviewsCount;
|
|
})
|
|
};
|
|
}
|
|
|
|
function ensureWorkingType(value: WorkingType): asserts value is WorkingTypeSchema {
|
|
if (value == WorkingType.book) return;
|
|
if (value == WorkingType.notepad) return;
|
|
assertUnreachable(value);
|
|
}
|
|
|
|
function ensureWorkingProgressStatus(
|
|
value: WorkingProgressStatus
|
|
): asserts value is WorkingProgressStatusSchema {
|
|
if (value == WorkingProgressStatus.inProgress) return;
|
|
if (value == WorkingProgressStatus.frozen) return;
|
|
if (value == WorkingProgressStatus.complete) return;
|
|
assertUnreachable(value);
|
|
}
|