From abea1fbd6f0c0251e8f1b60c157cbe6080a75ae7 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 6 Aug 2022 23:55:25 +0800 Subject: [PATCH] server: fix delete action --- .../server/src/services/document.service.ts | 1 + .../src/services/organization.service.ts | 8 +++---- packages/server/src/services/view.service.ts | 24 ++++++++++++++++--- packages/server/src/services/wiki.service.ts | 8 +++---- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/server/src/services/document.service.ts b/packages/server/src/services/document.service.ts index 16a94d8d..ebddecca 100644 --- a/packages/server/src/services/document.service.ts +++ b/packages/server/src/services/document.service.ts @@ -390,6 +390,7 @@ export class DocumentService { await Promise.all([ this.authService.deleteDocument(document.organizationId, document.wikiId, document.id), this.documentRepo.remove(document), + this.viewService.deleteDeletedDocumentView(user, document.organizationId, document.id), ]); } diff --git a/packages/server/src/services/organization.service.ts b/packages/server/src/services/organization.service.ts index 8dc5c164..8d2f282a 100644 --- a/packages/server/src/services/organization.service.ts +++ b/packages/server/src/services/organization.service.ts @@ -106,11 +106,9 @@ export class OrganizationService { wikiId: null, documentId: null, }); - await Promise.all([ - this.authService.deleteOrganization(organization.id), - this.organizationRepo.remove(organization), - this.wikiService.deleteOrganizationWiki(user, organizationId), - ]); + await this.wikiService.deleteOrganizationWiki(user, organizationId); + await this.organizationRepo.remove(organization); + await this.authService.deleteOrganization(organization.id); return organization; } diff --git a/packages/server/src/services/view.service.ts b/packages/server/src/services/view.service.ts index e7b1311e..6363dd1e 100644 --- a/packages/server/src/services/view.service.ts +++ b/packages/server/src/services/view.service.ts @@ -1,6 +1,5 @@ import { RedisDBEnum } from '@constants/*'; import { DocumentEntity } from '@entities/document.entity'; -import { UserEntity } from '@entities/user.entity'; import { buildRedis } from '@helpers/redis.helper'; import { Injectable } from '@nestjs/common'; import { IDocument, IOrganization, IUser } from '@think/domains'; @@ -73,7 +72,7 @@ export class ViewService { * @param user * @param document */ - private async recordUserVisitedDocumentInOrganization(user: UserEntity, document: DocumentEntity) { + private async recordUserVisitedDocumentInOrganization(user: IUser, document: DocumentEntity) { const { id: userId } = user; const { organizationId, id: documentId } = document; const key = this.buildVisitedDocumentInOrganizationKey(organizationId); @@ -113,11 +112,30 @@ export class ViewService { } } + /** + * 删除被删除文档的访问记录 + * @param user + * @param organizationId + * @param documentId + */ + public async deleteDeletedDocumentView( + user: IUser, + organizationId: IOrganization['id'], + documentId: IDocument['id'] + ) { + const { id: userId } = user; + const key = this.buildVisitedDocumentInOrganizationKey(organizationId); + const oldData = await this.redis.hget(key, userId); + + const filterData = (JSON.parse(oldData || '[]') || []).filter((record) => record.documentId !== documentId); + await this.redis.hset(key, userId, JSON.stringify(filterData)); + } + /** * 创建访问记录(内部调用,无公开接口) * @returns */ - async create(user: UserEntity | null, document: DocumentEntity) { + async create(user: IUser | null, document: DocumentEntity) { await Promise.all([ this.recordDocumentViews(document.id), user && this.recordUserVisitedDocumentInOrganization(user, document), diff --git a/packages/server/src/services/wiki.service.ts b/packages/server/src/services/wiki.service.ts index ecef94a2..76c06a2e 100644 --- a/packages/server/src/services/wiki.service.ts +++ b/packages/server/src/services/wiki.service.ts @@ -483,11 +483,9 @@ export class WikiService { wikiId: wiki.id, documentId: null, }); - await Promise.all([ - this.authService.deleteWiki(wiki.organizationId, wiki.id), - this.wikiRepo.remove(wiki), - this.documentService.deleteWikiDocuments(user, wikiId), - ]); + await this.wikiRepo.remove(wiki); + await this.documentService.deleteWikiDocuments(user, wikiId); + await this.authService.deleteWiki(wiki.organizationId, wiki.id); return wiki; }