-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new logging funct. to news module
- Loading branch information
1 parent
fc58e3f
commit f6470d7
Showing
4 changed files
with
84 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ describe(DeletionRequestUc.name, () => { | |
let filesStorageClientAdapterService: DeepMocked<FilesStorageClientAdapterService>; | ||
let dashboardService: DeepMocked<DashboardService>; | ||
let taskService: DeepMocked<TaskService>; | ||
let newsService: DeepMocked<NewsService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
|
@@ -147,6 +148,7 @@ describe(DeletionRequestUc.name, () => { | |
filesStorageClientAdapterService = module.get(FilesStorageClientAdapterService); | ||
dashboardService = module.get(DashboardService); | ||
taskService = module.get(TaskService); | ||
newsService = module.get(NewsService); | ||
await setupEntities(); | ||
}); | ||
|
||
|
@@ -243,6 +245,10 @@ describe(DeletionRequestUc.name, () => { | |
new ObjectId().toHexString(), | ||
]); | ||
|
||
const newsUpdated = DomainOperationBuilder.build(DomainName.LESSONS, OperationType.UPDATE, 1, [ | ||
new ObjectId().toHexString(), | ||
]); | ||
|
||
const parentEmail = '[email protected]'; | ||
|
||
const pseudonymsDeleted = DomainOperationBuilder.build(DomainName.PSEUDONYMS, OperationType.DELETE, 1, [ | ||
|
@@ -309,6 +315,7 @@ describe(DeletionRequestUc.name, () => { | |
taskService.removeCreatorIdFromTasks.mockResolvedValueOnce(tasksModifiedByRemoveCreatorId); | ||
taskService.removeUserFromFinished.mockResolvedValueOnce(tasksModifiedByRemoveUserFromFinished); | ||
taskService.deleteTasksByOnlyCreator.mockResolvedValueOnce(tasksDeleted); | ||
newsService.deleteCreatorOrUpdaterReference.mockResolvedValueOnce(newsUpdated); | ||
|
||
return { | ||
deletionRequestToExecute, | ||
|
@@ -536,6 +543,16 @@ describe(DeletionRequestUc.name, () => { | |
expect(taskService.removeUserFromFinished).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId); | ||
}); | ||
|
||
it('should call newsService.deleteCreatorOrUpdaterReference to update News without creatorId', async () => { | ||
const { deletionRequestToExecute } = setup(); | ||
|
||
deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]); | ||
|
||
await uc.executeDeletionRequests(); | ||
|
||
expect(newsService.deleteCreatorOrUpdaterReference).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId); | ||
}); | ||
|
||
it('should call deletionLogService.createDeletionLog to create logs for deletionRequest', async () => { | ||
const { deletionRequestToExecute } = setup(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,58 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DomainModel, EntityId, StatusModel } from '@shared/domain/types'; | ||
import { DomainName, EntityId, OperationType, StatusModel } from '@shared/domain/types'; | ||
import { Logger } from '@src/core/logger'; | ||
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable'; | ||
import { NewsRepo } from '@shared/repo'; | ||
import { DomainOperationBuilder } from '@shared/domain/builder'; | ||
import { DomainOperation } from '@shared/domain/interface'; | ||
import { News } from '@shared/domain/entity'; | ||
|
||
@Injectable() | ||
export class NewsService { | ||
constructor(private readonly newsRepo: NewsRepo, private readonly logger: Logger) { | ||
this.logger.setContext(NewsService.name); | ||
} | ||
|
||
public async deleteCreatorOrUpdaterReference(userId: EntityId): Promise<number> { | ||
public async deleteCreatorOrUpdaterReference(userId: EntityId): Promise<DomainOperation> { | ||
this.logger.info( | ||
new DataDeletionDomainOperationLoggable( | ||
'Deleting user data from News', | ||
DomainModel.NEWS, | ||
DomainName.NEWS, | ||
userId, | ||
StatusModel.PENDING | ||
) | ||
); | ||
|
||
const news = await this.newsRepo.findByCreatorOrUpdaterId(userId); | ||
const [newsWithUserData, counterOfNews] = await this.newsRepo.findByCreatorOrUpdaterId(userId); | ||
|
||
const newsCount = news[1]; | ||
if (newsCount === 0) { | ||
return 0; | ||
} | ||
|
||
news[0].forEach((newsEntity) => { | ||
newsWithUserData.forEach((newsEntity) => { | ||
newsEntity.removeCreatorReference(userId); | ||
newsEntity.removeUpdaterReference(userId); | ||
}); | ||
|
||
await this.newsRepo.save(news[0]); | ||
await this.newsRepo.save(newsWithUserData); | ||
|
||
const result = DomainOperationBuilder.build( | ||
DomainName.NEWS, | ||
OperationType.UPDATE, | ||
counterOfNews, | ||
this.getNewsId(newsWithUserData) | ||
); | ||
|
||
this.logger.info( | ||
new DataDeletionDomainOperationLoggable( | ||
'Successfully removed user data from News', | ||
DomainModel.NEWS, | ||
DomainName.NEWS, | ||
userId, | ||
StatusModel.FINISHED, | ||
newsCount, | ||
counterOfNews, | ||
0 | ||
) | ||
); | ||
return newsCount; | ||
return result; | ||
} | ||
|
||
private getNewsId(news: News[]): EntityId[] { | ||
return news.map((item) => item.id); | ||
} | ||
} |