Skip to content

Commit

Permalink
add new logging funct. to news module
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Jan 30, 2024
1 parent fc58e3f commit f6470d7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
17 changes: 17 additions & 0 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 8 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,15 @@ export class DeletionRequestUc {
}

private async removeUsersDataFromNews(deletionRequest: DeletionRequest) {
this.logger.debug({ action: 'removeUsersDataFromNews', deletionRequest });
const newsesModified = await this.newsService.deleteCreatorOrUpdaterReference(deletionRequest.targetRefId);

await this.logDeletion(deletionRequest, DomainModel.NEWS, DeletionOperationModel.UPDATE, newsesModified, 0);
await this.logDeletion(
deletionRequest,
newsesModified.domain,
newsesModified.operation,
newsesModified.count,
newsesModified.refs
);
}
}
42 changes: 36 additions & 6 deletions apps/server/src/modules/news/service/news.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { setupEntities, teamNewsFactory, userFactory } from '@shared/testing';
import { Logger } from '@src/core/logger';
import { NewsRepo } from '@shared/repo';
import { DomainOperationBuilder } from '@shared/domain/builder';
import { DomainName, OperationType } from '@shared/domain/types';
import { NewsService } from './news.service';

describe(NewsService.name, () => {
Expand Down Expand Up @@ -45,6 +47,7 @@ describe(NewsService.name, () => {
const setup = () => {
const user = userFactory.build();
const user2 = userFactory.build();
const anotherUserId = new ObjectId().toHexString();

const news1 = teamNewsFactory.build({
creator: user,
Expand All @@ -59,24 +62,51 @@ describe(NewsService.name, () => {
updater: user,
});

return { user, news1, news2, news3 };
const expectedResultWithUpdatedNews = DomainOperationBuilder.build(DomainName.NEWS, OperationType.UPDATE, 3, [
news1.id,
news2.id,
news3.id,
]);

const expectedResultWithoutUpdatedNews = DomainOperationBuilder.build(
DomainName.NEWS,
OperationType.UPDATE,
0,
[]
);

return {
anotherUserId,
expectedResultWithUpdatedNews,
expectedResultWithoutUpdatedNews,
user,
news1,
news2,
news3,
};
};
it('should successfully delete creator or updater reference from news', async () => {
const { user, news1, news2, news3 } = setup();
const { expectedResultWithUpdatedNews, user, news1, news2, news3 } = setup();

repo.findByCreatorOrUpdaterId.mockResolvedValueOnce([[news1, news2, news3], 3]);

const result = await service.deleteCreatorOrUpdaterReference(user.id);

expect(news1.creator).toBeUndefined();
expect(news1.updater).toBeUndefined();
expect(news2.creator).toBeUndefined();
expect(news3.updater).toBeUndefined();
expect(result).toBe(3);
expect(result).toEqual(expectedResultWithUpdatedNews);
});

it('should return 0 if news not found', async () => {
const anotherUser = new ObjectId().toHexString();
const { anotherUserId, expectedResultWithoutUpdatedNews } = setup();

repo.findByCreatorOrUpdaterId.mockResolvedValueOnce([[], 0]);
const result = await service.deleteCreatorOrUpdaterReference(anotherUser);
expect(result).toBe(0);

const result = await service.deleteCreatorOrUpdaterReference(anotherUserId);

expect(result).toEqual(expectedResultWithoutUpdatedNews);
});
});
});
37 changes: 23 additions & 14 deletions apps/server/src/modules/news/service/news.service.ts
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);
}
}

0 comments on commit f6470d7

Please sign in to comment.