Skip to content

Commit e876e86

Browse files
authored
chore: log trashed items (#1035)
1 parent 38cd005 commit e876e86

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/modules/trash/trash.controller.spec.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { TrashUseCases } from './trash.usecase';
77
import { newUser, newFile, newFolder } from '../../../test/fixtures';
88
import {
99
BadRequestException,
10-
type Logger,
10+
Logger,
1111
NotFoundException,
1212
InternalServerErrorException,
1313
} from '@nestjs/common';
14+
import { PLAN_FREE_INDIVIDUAL_TIER_LABEL } from '../feature-limit/limits.enum';
15+
import { Tier } from '../feature-limit/domain/tier.domain';
1416
import { ItemToTrashType } from './dto/controllers/move-items-to-trash.dto';
1517
import {
1618
DeleteItemType,
@@ -172,6 +174,69 @@ describe('TrashController', () => {
172174
expect(fileUseCases.moveFilesToTrash).toHaveBeenCalled();
173175
});
174176

177+
it('When user is on a paid tier, then it should log trashed file and folder uuids', async () => {
178+
const paidTier = Tier.build({ id: v4(), label: 'premium' });
179+
const fileUuid = v4();
180+
const folderUuid = v4();
181+
182+
jest.spyOn(fileUseCases, 'moveFilesToTrash').mockResolvedValue();
183+
jest.spyOn(folderUseCases, 'moveFoldersToTrash').mockResolvedValue();
184+
jest
185+
.spyOn(userUseCases, 'getWorkspaceMembersByBrigeUser')
186+
.mockResolvedValue([]);
187+
const logSpy = jest.spyOn(Logger.prototype, 'log');
188+
189+
await controller.moveItemsToTrash(
190+
{
191+
items: [
192+
{ uuid: fileUuid, type: ItemToTrashType.FILE },
193+
{ uuid: folderUuid, type: ItemToTrashType.FOLDER },
194+
],
195+
},
196+
user,
197+
paidTier,
198+
'clientId',
199+
'1.0.0',
200+
requester,
201+
);
202+
203+
expect(logSpy).toHaveBeenCalledWith(
204+
{ user: user.uuid, fileUuids: [fileUuid], folderUuids: [folderUuid] },
205+
'User trashed items',
206+
);
207+
});
208+
209+
it('When user is on the free tier, then it should not log trashed items', async () => {
210+
const freeTier = Tier.build({
211+
id: v4(),
212+
label: PLAN_FREE_INDIVIDUAL_TIER_LABEL,
213+
});
214+
const fileUuid = v4();
215+
216+
jest.spyOn(fileUseCases, 'moveFilesToTrash').mockResolvedValue();
217+
jest.spyOn(folderUseCases, 'moveFoldersToTrash').mockResolvedValue();
218+
jest
219+
.spyOn(userUseCases, 'getWorkspaceMembersByBrigeUser')
220+
.mockResolvedValue([]);
221+
const logSpy = jest.spyOn(Logger.prototype, 'log');
222+
223+
await controller.moveItemsToTrash(
224+
{
225+
items: [{ uuid: fileUuid, type: ItemToTrashType.FILE }],
226+
},
227+
user,
228+
freeTier,
229+
'clientId',
230+
'1.0.0',
231+
requester,
232+
);
233+
234+
expect(logSpy).not.toHaveBeenCalledWith(
235+
expect.objectContaining({ fileUuids: expect.any(Array) }),
236+
'User trashed items',
237+
);
238+
});
239+
175240
it('When an unexpected error occurs, then it should throw InternalServerErrorException', async () => {
176241
const fileItems = [
177242
{

src/modules/trash/trash.controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
} from './trash-expiration.utils';
5858
import { AuditAction } from '../../common/audit-logs/audit-logs.attributes';
5959
import { AuditLog } from '../../common/audit-logs/decorators/audit-log.decorator';
60+
import { PLAN_FREE_INDIVIDUAL_TIER_LABEL } from '../feature-limit/limits.enum';
6061

6162
@ApiTags('Trash')
6263
@Controller('storage/trash')
@@ -224,6 +225,14 @@ export class TrashController {
224225
this.folderUseCases.moveFoldersToTrash(user, folderIds, folderUuids),
225226
]);
226227

228+
const isPaidUser = tier && tier.label !== PLAN_FREE_INDIVIDUAL_TIER_LABEL;
229+
if (isPaidUser) {
230+
this.logger.log(
231+
{ user: user.uuid, fileUuids, folderUuids },
232+
'User trashed items',
233+
);
234+
}
235+
227236
this.userUseCases
228237
.getWorkspaceMembersByBrigeUser(user.bridgeUser)
229238
.then((members) => {

0 commit comments

Comments
 (0)