Allow setting createdAt, emoji properties through documents.create (#5864)

This commit is contained in:
Tom Moor
2023-09-21 22:37:27 -04:00
committed by GitHub
parent 8833e578f1
commit 76862b626b
4 changed files with 60 additions and 11 deletions

View File

@@ -75,7 +75,7 @@ export default async function documentCreator({
teamId: user.teamId,
userId: user.id,
createdAt,
updatedAt,
updatedAt: updatedAt ?? createdAt,
lastModifiedById: user.id,
createdById: user.id,
template,

View File

@@ -1,4 +1,4 @@
import { subDays } from "date-fns";
import { addMinutes, subDays } from "date-fns";
import { CollectionPermission } from "@shared/types";
import {
Document,
@@ -2537,6 +2537,38 @@ describe("#documents.create", () => {
expect(res.status).toEqual(200);
});
it("should succeed with specific createdAt date in the past", async () => {
const user = await buildUser();
const createdAt = new Date().toISOString();
const res = await server.post("/api/documents.create", {
body: {
token: user.getJwtToken(),
collectionId: null,
title: "new document",
createdAt,
text: "hello",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.createdAt).toEqual(createdAt);
expect(body.data.updatedAt).toEqual(createdAt);
});
it("should fail with createdAt date in the future", async () => {
const user = await buildUser();
const res = await server.post("/api/documents.create", {
body: {
token: user.getJwtToken(),
collectionId: null,
title: "new document",
createdAt: addMinutes(new Date(), 1).toISOString(),
text: "hello",
},
});
expect(res.status).toEqual(400);
});
it("should fail for invalid parentDocumentId", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
@@ -2569,6 +2601,7 @@ describe("#documents.create", () => {
body: {
token: user.getJwtToken(),
collectionId: collection.id,
emoji: "🚢",
title: "new document",
text: "hello",
publish: true,
@@ -2579,6 +2612,7 @@ describe("#documents.create", () => {
expect(res.status).toEqual(200);
expect(newDocument!.parentDocumentId).toBe(null);
expect(newDocument!.collectionId).toBe(collection.id);
expect(newDocument!.emoji).toBe("🚢");
expect(body.policies[0].abilities.update).toEqual(true);
});

View File

@@ -1289,14 +1289,16 @@ router.post(
transaction(),
async (ctx: APIContext<T.DocumentsCreateReq>) => {
const {
title = "",
text = "",
title,
text,
emoji,
publish,
collectionId,
parentDocumentId,
fullWidth,
templateId,
template,
createdAt,
} = ctx.input.body;
const editorVersion = ctx.headers["x-editor-version"] as string | undefined;
@@ -1343,6 +1345,8 @@ router.post(
const document = await documentCreator({
title,
text,
emoji,
createdAt,
publish,
collectionId,
parentDocumentId,

View File

@@ -280,28 +280,39 @@ export type DocumentsImportReq = z.infer<typeof DocumentsImportSchema>;
export const DocumentsCreateSchema = BaseSchema.extend({
body: z.object({
/** Doc title */
/** Document title */
title: z.string().default(""),
/** Doc text */
/** Document text */
text: z.string().default(""),
/** Emoji displayed alongside doc title */
emoji: z.string().regex(emojiRegex()).optional(),
/** Boolean to denote if the doc should be published */
publish: z.boolean().optional(),
/** Create Doc under this collection */
/** Collection to create document within */
collectionId: z.string().uuid().nullish(),
/** Create Doc under this parent */
/** Parent document to create within */
parentDocumentId: z.string().uuid().nullish(),
/** Create doc with this template */
/** A template to create the document from */
templateId: z.string().uuid().optional(),
/** Boolean to denote if the doc should occupy full width */
/** Optionally set the created date in the past */
createdAt: z.coerce
.date()
.optional()
.refine((data) => !data || data < new Date(), {
message: "createdAt must be in the past",
}),
/** Boolean to denote if the document should occupy full width */
fullWidth: z.boolean().optional(),
/** Whether to create a template doc */
/** Whether this should be considered a template */
template: z.boolean().optional(),
}),
})