Updates on server to support drafts with no collection

This commit is contained in:
Saumya Pandey committed 2021-08-06 23:18:53 +05:30
1 parent 57a2524fbd
commit 4e29b14426
3 files changed
+77 -36

No files matched your search

+33 -27
View File
@@ -431,13 +431,8 @@ router.post("documents.drafts", auth(), pagination(), async (ctx) => {
authorize(user, "read", collection);
}
const collectionIds = !!collectionId
? [collectionId]
: await user.collectionIds();
const whereConditions = {
userId: user.id,
collectionId: collectionIds,
publishedAt: { [Op.eq]: null },
updatedAt: undefined,
};
@@ -1039,6 +1034,7 @@ router.post("documents.update", auth(), async (ctx) => {
transaction = await sequelize.transaction();
if (publish) {
ctx.assertPresent(collection, "Collection should be present");
await document.publish(user.id, { transaction });
} else {
await document.save({ autosave, transaction });
@@ -1194,7 +1190,8 @@ router.post("documents.delete", auth(), async (ctx) => {
});
authorize(user, "permanentDelete", document);
await Document.update(
//unscoping to apply on drafts documents
await Document.unscoped().update(
{ parentDocumentId: null },
{
where: {
@@ -1346,7 +1343,9 @@ router.post("documents.create", auth(), async (ctx) => {
} = ctx.body;
const editorVersion = ctx.headers["x-editor-version"];
ctx.assertUuid(collectionId, "collectionId must be an uuid");
if (collectionId) {
ctx.assertUuid(collectionId, "collectionId must be an uuid");
}
if (parentDocumentId) {
ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid");
}
@@ -1356,31 +1355,38 @@ router.post("documents.create", auth(), async (ctx) => {
const user = ctx.state.user;
authorize(user, "createDocument", user.team);
const collection = await Collection.scope({
method: ["withMembership", user.id],
}).findOne({
where: {
id: collectionId,
teamId: user.teamId,
},
});
authorize(user, "publish", collection);
let collection;
let parentDocument;
if (parentDocumentId) {
parentDocument = await Document.findOne({
let templateDocument;
if (collectionId) {
collection = await Collection.scope({
method: ["withMembership", user.id],
}).findOne({
where: {
id: parentDocumentId,
collectionId: collection.id,
id: collectionId,
teamId: user.teamId,
},
});
authorize(user, "read", parentDocument, { collection });
}
let templateDocument;
if (templateId) {
templateDocument = await Document.findByPk(templateId, { userId: user.id });
authorize(user, "read", templateDocument);
authorize(user, "publish", collection);
if (parentDocumentId) {
parentDocument = await Document.findOne({
where: {
id: parentDocumentId,
collectionId: collection.id,
},
});
authorize(user, "read", parentDocument, { collection });
}
if (templateId) {
templateDocument = await Document.findByPk(templateId, {
userId: user.id,
});
authorize(user, "read", templateDocument);
}
}
const document = await documentCreator({
+32 -3
View File
@@ -197,14 +197,43 @@ Collection.associate = (models) => {
};
Collection.addHook("afterDestroy", async (model: Collection) => {
// sets deletedAt attribute of archived, templates and normal documents
await Document.destroy({
where: {
collectionId: model.id,
archivedAt: {
[Op.eq]: null,
},
},
});
// updating the documents that moved to deleted state from archived state
await Document.update(
{
archivedAt: null,
},
{
where: {
deletedAt: {
[Op.ne]: null,
},
},
paranoid: false,
}
);
// updating the drafts to have no collection
await Document.unscoped().update(
{
collectionId: null,
parentDocumentId: null,
},
{
where: {
publishedAt: {
[Op.eq]: null,
},
collectionId: model.id,
},
}
);
});
Collection.addHook("afterCreate", (model: Collection, options) => {
+12 -6
View File
@@ -24,11 +24,8 @@ allow(User, ["star", "unstar"], Document, (user, document) => {
if (document.deletedAt) return false;
if (document.template) return false;
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "read", document.collection)) return false;
if (document.collection && cannot(user, "read", document.collection))
return false;
return user.teamId === document.teamId;
});
@@ -37,6 +34,10 @@ allow(User, "share", Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "share", document.collection)) {
return false;
}
@@ -48,7 +49,12 @@ allow(User, "update", Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
if (cannot(user, "update", document.collection)) {
// if a document has a collection and it is published, it is everything except the draft.
if (
document.collection &&
document.publishedAt &&
cannot(user, "update", document.collection)
) {
return false;
}