fix: Templatize no longer works, closes #6067

This commit is contained in:
Tom Moor
2023-10-25 20:29:36 -04:00
parent f063bef968
commit 2fcf9149b5
2 changed files with 40 additions and 25 deletions

View File

@@ -23,7 +23,7 @@ function DocumentTemplatizeDialog({ documentId }: Props) {
const template = await document?.templatize(); const template = await document?.templatize();
if (template) { if (template) {
history.push(documentPath(template)); history.push(documentPath(template));
toast.message(t("Template created, go ahead and customize it")); toast.success(t("Template created, go ahead and customize it"));
} }
}, [document, history, t]); }, [document, history, t]);

View File

@@ -885,46 +885,61 @@ router.post(
auth({ member: true }), auth({ member: true }),
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
validate(T.DocumentsTemplatizeSchema), validate(T.DocumentsTemplatizeSchema),
transaction(),
async (ctx: APIContext<T.DocumentsTemplatizeReq>) => { async (ctx: APIContext<T.DocumentsTemplatizeReq>) => {
const { id } = ctx.input.body; const { id } = ctx.input.body;
const { user } = ctx.state.auth; const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const original = await Document.findByPk(id, { const original = await Document.findByPk(id, {
userId: user.id, userId: user.id,
transaction,
}); });
authorize(user, "update", original); authorize(user, "update", original);
const document = await Document.create({ const document = await Document.create(
editorVersion: original.editorVersion, {
collectionId: original.collectionId, editorVersion: original.editorVersion,
teamId: original.teamId, collectionId: original.collectionId,
userId: user.id, teamId: original.teamId,
publishedAt: new Date(), userId: user.id,
lastModifiedById: user.id, publishedAt: new Date(),
createdById: user.id, lastModifiedById: user.id,
template: true, createdById: user.id,
emoji: original.emoji,
title: original.title,
text: original.text,
});
await Event.create({
name: "documents.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
data: {
title: document.title,
template: true, template: true,
emoji: original.emoji,
title: original.title,
text: original.text,
}, },
ip: ctx.request.ip, {
}); transaction,
}
);
await Event.create(
{
name: "documents.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
data: {
title: document.title,
template: true,
},
ip: ctx.request.ip,
},
{
transaction,
}
);
// reload to get all of the data needed to present (user, collection etc) // reload to get all of the data needed to present (user, collection etc)
const reloaded = await Document.findByPk(document.id, { const reloaded = await Document.findByPk(document.id, {
userId: user.id, userId: user.id,
rejectOnEmpty: true, transaction,
}); });
invariant(reloaded, "document not found");
ctx.body = { ctx.body = {
data: await presentDocument(reloaded), data: await presentDocument(reloaded),