Skip to content

Commit

Permalink
Merge pull request #401 from internxt/feat/creation-modification-time…
Browse files Browse the repository at this point in the history
…s-on-folders

[PB-3009]: feat/creation modification times on folders
  • Loading branch information
apsantiso authored Oct 2, 2024
2 parents cb509af + 29d3f17 commit 9c20e59
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const tableName = 'folders';
const newColumn1 = 'creation_time';
const newColumn2 = 'modification_time';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(tableName, newColumn1, {
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW'),
allowNull: false,
});

await queryInterface.addColumn(tableName, newColumn2, {
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW'),
allowNull: false,
});
},

async down(queryInterface) {
await queryInterface.removeColumn(tableName, newColumn1);
await queryInterface.removeColumn(tableName, newColumn2);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const tableName = 'files';
const indexName = 'files_modification_time_index';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(
`CREATE INDEX CONCURRENTLY ${indexName} ON ${tableName} (modification_time);`,
);
},

async down(queryInterface) {
await queryInterface.sequelize.query(`DROP INDEX IF EXISTS ${indexName};`);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const tableName = 'folders';
const indexName = 'folders_modification_time_index';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(
`CREATE INDEX CONCURRENTLY ${indexName} ON ${tableName} (modification_time);`,
);
},

async down(queryInterface) {
await queryInterface.sequelize.query(`DROP INDEX IF EXISTS ${indexName};`);
},
};
5 changes: 5 additions & 0 deletions src/modules/file/dto/create-file.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class CreateFileDto {
@IsOptional()
date?: Date;

@ApiProperty({
description: 'The creation time of the file (optional)',
required: false,
example: '2023-05-30T12:34:56.789Z',
})
@ApiProperty({
description: 'The date associated with the file (optional)',
required: false,
Expand Down
20 changes: 18 additions & 2 deletions src/modules/file/file.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ describe('FileUseCases', () => {
...mockFile,
plainName: newFileMeta.plainName,
name: encryptedName,
modificationTime: new Date(),
},
});

Expand Down Expand Up @@ -781,9 +782,24 @@ describe('FileUseCases', () => {
expect(fileRepository.updateByUuidAndUserId).toHaveBeenCalledWith(
mockFile.uuid,
userMocked.id,
{ plainName: newFileMeta.plainName, name: encryptedName },
expect.objectContaining({
plainName: newFileMeta.plainName,
name: encryptedName,
}),
);
const {
modificationTime: _resultModificationTime,
...resultWithoutModificationTime
} = result;
const {
modificationTime: updatedFileModificationTime,
...updatedFileWithoutModificationTime
} = updatedFile;

expect(resultWithoutModificationTime).toEqual(
updatedFileWithoutModificationTime,
);
expect(result).toEqual(updatedFile);
expect(mockFile).not.toBe(updatedFileModificationTime);
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/modules/file/file.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,19 @@ export class FileUseCases {
);
}

const modificationTime = new Date();

await this.fileRepository.updateByUuidAndUserId(file.uuid, user.id, {
plainName: newFileMetada.plainName,
name: cryptoFileName,
modificationTime: modificationTime,
});

return {
...file.toJSON(),
name: cryptoFileName,
plainName: newFileMetada.plainName,
modificationTime,
};
}

Expand Down
20 changes: 19 additions & 1 deletion src/modules/folder/dto/create-folder.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsUUID } from 'class-validator';
import { IsDateString, IsNotEmpty, IsOptional, IsUUID } from 'class-validator';
import { FolderAttributes } from '../../folder/folder.attributes';

export class CreateFolderDto {
Expand All @@ -17,4 +17,22 @@ export class CreateFolderDto {
@IsNotEmpty()
@IsUUID('4')
parentFolderUuid: FolderAttributes['uuid'];

@ApiProperty({
description: 'The last modification time of the folder (optional)',
required: false,
example: '2023-05-30T12:34:56.789Z',
})
@IsDateString()
@IsOptional()
modificationTime?: Date;

@ApiProperty({
description: 'The creation time of the folder (optional)',
required: false,
example: '2023-05-30T12:34:56.789Z',
})
@IsDateString()
@IsOptional()
creationTime?: Date;
}
2 changes: 2 additions & 0 deletions src/modules/folder/folder.attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ export interface FolderAttributes {
createdAt: Date;
updatedAt: Date;
removedAt: Date;
creationTime: Date;
modificationTime: Date;
sharings?: Sharing[];
}
4 changes: 1 addition & 3 deletions src/modules/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ export class FolderController {
@Body() createFolderDto: CreateFolderDto,
@Client() clientId: string,
) {
const { plainName, parentFolderUuid } = createFolderDto;
const folder = await this.folderUseCases.createFolder(
user,
plainName,
parentFolderUuid,
createFolderDto,
);

this.storageNotificationService.folderCreated({
Expand Down
6 changes: 6 additions & 0 deletions src/modules/folder/folder.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class Folder implements FolderAttributes {
createdAt: Date;
updatedAt: Date;
size: number;
creationTime: Date;
modificationTime: Date;
sharings?: Sharing[];

private constructor({
Expand All @@ -59,6 +61,8 @@ export class Folder implements FolderAttributes {
removed,
removedAt,
sharings,
creationTime,
modificationTime,
}: FolderAttributes) {
this.type = 'folder';
this.id = id;
Expand All @@ -80,6 +84,8 @@ export class Folder implements FolderAttributes {
this.removed = removed;
this.removedAt = removedAt;
this.sharings = sharings;
this.creationTime = creationTime;
this.modificationTime = modificationTime;
}

static build(folder: FolderAttributes): Folder {
Expand Down
9 changes: 9 additions & 0 deletions src/modules/folder/folder.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FolderAttributes } from './folder.attributes';
import { SharingModel } from '../sharing/models';
import { Sharing } from '../sharing/sharing.domain';
import { WorkspaceItemUserModel } from '../workspaces/models/workspace-items-users.model';
import { Sequelize } from 'sequelize';

@Table({
underscored: true,
Expand Down Expand Up @@ -74,6 +75,14 @@ export class FolderModel extends Model implements FolderAttributes {
@Column
removed: boolean;

@Default(Sequelize.fn('NOW'))
@Column
creationTime: Date;

@Default(Sequelize.fn('NOW'))
@Column
modificationTime: Date;

@AllowNull
@Column
deletedAt: Date;
Expand Down
Loading

0 comments on commit 9c20e59

Please sign in to comment.