fix: Reassign user on unpublish (#1857)

* findOne -> findByPk
This commit is contained in:
Tom Moor
2021-01-30 18:31:08 -08:00
committed by GitHub
parent eeb7650941
commit 91ee3e62f2
5 changed files with 37 additions and 9 deletions

View File

@@ -575,24 +575,30 @@ Document.prototype.archiveWithChildren = async function (userId, options) {
return this.save(options);
};
Document.prototype.publish = async function (options) {
Document.prototype.publish = async function (userId: string, options) {
if (this.publishedAt) return this.save(options);
const collection = await Collection.findByPk(this.collectionId);
await collection.addDocumentToStructure(this, 0);
this.lastModifiedById = userId;
this.publishedAt = new Date();
await this.save(options);
return this;
};
Document.prototype.unpublish = async function (options) {
Document.prototype.unpublish = async function (userId: string, options) {
if (!this.publishedAt) return this;
const collection = await this.getCollection();
await collection.removeDocumentInStructure(this);
// unpublishing a document converts the "ownership" to yourself, so that it
// can appear in your drafts rather than the original creators
this.userId = userId;
this.lastModifiedById = userId;
this.publishedAt = null;
await this.save(options);