Deleting a collection should detach associated drafts from it (#5082)

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
Apoorv Mishra
2023-04-24 00:50:44 +05:30
committed by GitHub
parent 7250c0ed64
commit 86062f396d
39 changed files with 363 additions and 112 deletions

View File

@@ -267,7 +267,8 @@ class Document extends ParanoidModel {
model.archivedAt ||
model.template ||
!model.publishedAt ||
!model.changed("title")
!model.changed("title") ||
!model.collectionId
) {
return;
}
@@ -286,12 +287,17 @@ class Document extends ParanoidModel {
@AfterCreate
static async addDocumentToCollectionStructure(model: Document) {
if (model.archivedAt || model.template || !model.publishedAt) {
if (
model.archivedAt ||
model.template ||
!model.publishedAt ||
!model.collectionId
) {
return;
}
return this.sequelize!.transaction(async (transaction: Transaction) => {
const collection = await Collection.findByPk(model.collectionId, {
const collection = await Collection.findByPk(model.collectionId!, {
transaction,
lock: transaction.LOCK.UPDATE,
});
@@ -399,7 +405,7 @@ class Document extends ParanoidModel {
@ForeignKey(() => Collection)
@Column(DataType.UUID)
collectionId: string;
collectionId?: string | null;
@HasMany(() => Revision)
revisions: Revision[];
@@ -555,13 +561,21 @@ class Document extends ParanoidModel {
return this.save(options);
};
publish = async (userId: string, { transaction }: SaveOptions<Document>) => {
publish = async (
userId: string,
collectionId: string,
{ transaction }: SaveOptions<Document>
) => {
// If the document is already published then calling publish should act like
// a regular save
if (this.publishedAt) {
return this.save({ transaction });
}
if (!this.collectionId) {
this.collectionId = collectionId;
}
if (!this.template) {
const collection = await Collection.findByPk(this.collectionId, {
transaction,
@@ -587,10 +601,12 @@ class Document extends ParanoidModel {
}
await this.sequelize.transaction(async (transaction: Transaction) => {
const collection = await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
});
const collection = this.collectionId
? await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
})
: undefined;
if (collection) {
await collection.removeDocumentInStructure(this, { transaction });
@@ -610,10 +626,12 @@ class Document extends ParanoidModel {
// to the archived area, where it can be subsequently restored.
archive = async (userId: string) => {
await this.sequelize.transaction(async (transaction: Transaction) => {
const collection = await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
});
const collection = this.collectionId
? await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
})
: undefined;
if (collection) {
await collection.removeDocumentInStructure(this, { transaction });
@@ -628,10 +646,12 @@ class Document extends ParanoidModel {
// Restore an archived document back to being visible to the team
unarchive = async (userId: string) => {
await this.sequelize.transaction(async (transaction: Transaction) => {
const collection = await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
});
const collection = this.collectionId
? await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
})
: undefined;
// check to see if the documents parent hasn't been archived also
// If it has then restore the document to the collection root.
@@ -675,6 +695,7 @@ class Document extends ParanoidModel {
const collection = await Collection.findByPk(this.collectionId, {
transaction,
lock: transaction.LOCK.UPDATE,
paranoid: false,
});
await collection?.deleteDocument(this, { transaction });
} else {

View File

@@ -252,7 +252,9 @@ class Team extends ParanoidModel {
},
{ transaction }
);
await document.publish(collection.createdById, { transaction });
await document.publish(collection.createdById, collection.id, {
transaction,
});
}
});
};

View File

@@ -164,11 +164,12 @@ export default class NotificationHelper {
const collectionIds = await recipient.collectionIds();
// Check the recipient has access to the collection this document is in. Just
// because they are subscribed doesn't meant they "still have access to read
// because they are subscribed doesn't mean they still have access to read
// the document.
if (
recipient.email &&
!recipient.isSuspended &&
document.collectionId &&
collectionIds.includes(document.collectionId)
) {
filtered.push(recipient);

View File

@@ -31,7 +31,7 @@ type SearchOptions = {
/** The query offset for pagination */
offset?: number;
/** Limit results to a collection. Authorization is presumed to have been done before passing to this helper. */
collectionId?: string;
collectionId?: string | null;
/** Limit results to a shared document. */
share?: Share;
/** Limit results to a date range. */