Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/modules/feature-limit/feature-limit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { LimitLabels } from './limits.enum';
import { PlatformName } from '../../common/constants';
import { SequelizeWorkspaceRepository } from '../workspaces/repositories/workspaces.repository';
import { SequelizeUserRepository } from '../user/user.repository';
import { Limit } from './domain/limit.domain';
import { User } from '../user/user.domain';

@Injectable()
export class FeatureLimitService {
Expand Down Expand Up @@ -146,4 +148,13 @@ export class FeatureLimitService {
maxVersions: Number(limitsMap.get(LimitLabels.FileVersionMaxNumber)) || 0,
};
}

async getUserLimitByLabel(label: LimitLabels, user: User): Promise<Limit> {
const [userOverriddenLimits, tierLimits] = await Promise.all([
this.limitsRepository.findUserOverriddenLimit(user.uuid, label),
this.limitsRepository.findLimitByLabelAndTier(user.tierId, label),
]);

return userOverriddenLimits ?? tierLimits;
}
}
1 change: 1 addition & 0 deletions src/modules/feature-limit/limits.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum LimitLabels {
FileVersionMaxSize = 'file-version-max-size',
FileVersionRetentionDays = 'file-version-retention-days',
FileVersionMaxNumber = 'file-version-max-number',
MaxZeroSizeFiles = 'max-zero-size-files',
}

export enum LimitTypes {
Expand Down
15 changes: 12 additions & 3 deletions src/modules/file/dto/create-file.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsDateString,
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,
IsString,
IsUUID,
MaxLength,
ValidateIf,
} from 'class-validator';

export class CreateFileDto {
Expand All @@ -16,11 +20,15 @@ export class CreateFileDto {
bucket: string;

@ApiProperty({
description: 'The ID of the file',
description: 'The ID of the file (required when size > 0)',
example: 'file12345',
required: false,
})
@IsString()
fileId: string;
@ValidateIf((o) => o.size > 0)
@IsNotEmpty({ message: 'fileId is required when size is greater than 0' })
// Max varchar length in the database is 24
@MaxLength(24)
fileId?: string;

@ApiProperty({
description: 'The encryption version used for the file',
Expand All @@ -44,6 +52,7 @@ export class CreateFileDto {
format: 'bigint',
example: 123456789,
})
@IsPositive()
@IsNumber()
size: bigint;

Expand Down
19 changes: 14 additions & 5 deletions src/modules/file/dto/replace-file.dto.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import {
IsDateString,
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,
IsString,
MaxLength,
Min,
ValidateIf,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class ReplaceFileDto {
@IsNotEmpty()
@IsString()
@ApiProperty({
example: '651300a2da9b27001f63f384',
description: 'File id',
description: 'File id (required when size > 0)',
required: false,
})
fileId: string;
@ValidateIf((o) => o.size > 0)
@IsNotEmpty({ message: 'fileId is required when size is greater than 0' })
@IsString()
// Max varchar length in the database is 24
@MaxLength(24)
fileId?: string;

@IsPositive()
@ApiProperty({
example: '3005',
description: 'New file size',
})
@IsPositive()
@IsNumber()
size: bigint;

@IsDateString()
Expand Down
25 changes: 25 additions & 0 deletions src/modules/file/file.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,29 @@ describe('FileRepository', () => {
expect(response).toBe(0);
});
});

describe('getZeroSizeFilesCountByUser', () => {
it('When user zero-size files are requested, then it should make the query with expected parameters', async () => {
const userId = 123;
const count = 2;

jest.spyOn(fileModel, 'findAndCountAll').mockResolvedValueOnce({
rows: [],
count,
} as any);

const result = await repository.getZeroSizeFilesCountByUser(userId);

expect(fileModel.findAndCountAll).toHaveBeenCalledWith({
where: {
userId,
size: 0,
status: {
[Op.not]: FileStatus.DELETED,
},
},
});
expect(result).toBe(count);
});
});
});
15 changes: 15 additions & 0 deletions src/modules/file/file.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface FileRepository {
): Promise<void>;
getFilesWhoseFolderIdDoesNotExist(userId: File['userId']): Promise<number>;
getFilesCountWhere(where: Partial<File>): Promise<number>;
getZeroSizeFilesCountByUser(userId: User['id']): Promise<number>;
updateFilesStatusToTrashed(
user: User,
fileIds: File['fileId'][],
Expand Down Expand Up @@ -744,6 +745,20 @@ export class SequelizeFileRepository implements FileRepository {
return count;
}

async getZeroSizeFilesCountByUser(userId: User['id']): Promise<number> {
const { count } = await this.fileModel.findAndCountAll({
where: {
userId,
size: 0,
status: {
[Op.not]: FileStatus.DELETED,
},
},
});

return count;
}

async updateFilesStatusToTrashed(
user: User,
fileIds: File['fileId'][],
Expand Down
Loading
Loading