mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 16:28:37 +01:00
new selfhosted version
This commit is contained in:
@@ -1,36 +1,32 @@
|
||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||
|
||||
import { ProjectSnapshotModel } from "@schema/project/ProjectSnapshot";
|
||||
import { z } from "zod";
|
||||
|
||||
|
||||
const ZCreateSnapshotBody = z.object({
|
||||
name: z.string().trim().min(2).max(22),
|
||||
color: z.string().regex(/^#[0-9A-Fa-f]{6}$/, {
|
||||
message: "Color must be a hex code starting with # followed by 6 hex digits"
|
||||
}),
|
||||
from: z.string(),
|
||||
to: z.string()
|
||||
})
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false, allowGuests: true, requireRange: false });
|
||||
if (!data) return;
|
||||
const ctx = await getRequestContext(event, 'pid', 'permission:member');
|
||||
const { project_id } = ctx;
|
||||
|
||||
const body = await readBody(event);
|
||||
const body = await readValidatedBody(event, ZCreateSnapshotBody.parse);
|
||||
|
||||
const { name: newSnapshotName, from, to, color: snapshotColor } = body;
|
||||
|
||||
if (!newSnapshotName) return setResponseStatus(event, 400, 'SnapshotName too short');
|
||||
if (newSnapshotName.trim().length == 0) return setResponseStatus(event, 400, 'SnapshotName too short');
|
||||
|
||||
if (!from) return setResponseStatus(event, 400, 'from is required');
|
||||
if (!to) return setResponseStatus(event, 400, 'to is required');
|
||||
if (!snapshotColor) return setResponseStatus(event, 400, 'color is required');
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
|
||||
const project = await ProjectModel.findById(data.project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
|
||||
const newSnapshot = await ProjectSnapshotModel.create({
|
||||
name: newSnapshotName.trim(),
|
||||
name: newSnapshotName,
|
||||
from: new Date(from),
|
||||
to: new Date(to),
|
||||
color: snapshotColor,
|
||||
project_id: data.project_id
|
||||
project_id
|
||||
});
|
||||
|
||||
return newSnapshot.id;
|
||||
|
||||
Reference in New Issue
Block a user