Skip to content

Commit

Permalink
feat: improve buckets error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Sep 1, 2024
1 parent 2883d4d commit 016d173
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type FindResourcesMetadataQueryHandlerPayload,
type FindResourcesMetadataQueryHandlerResult,
} from './findResourcesMetadataQueryHandler.js';
import { OperationNotValidError } from '../../../../../common/errors/common/operationNotValidError.js';
import { type LoggerService } from '../../../../../libs/logger/services/loggerService/loggerService.js';
import { ForbiddenAccessError } from '../../../../authModule/application/errors/forbiddenAccessError.js';
import { type FindUserBucketsQueryHandler } from '../../../../userModule/application/queryHandlers/findUserBucketsQueryHandler/findUserBucketsQueryHandler.js';
Expand Down Expand Up @@ -33,6 +34,16 @@ export class FindResourcesMetadataQueryHandlerImpl implements FindResourcesMetad
});
}

const bucketExists = await this.resourceBlobSerice.bucketExists({ bucketName });

if (!bucketExists) {
throw new OperationNotValidError({
reason: 'Bucket does not exist.',
userId,
bucketName,
});
}

this.loggerService.debug({
message: 'Fetching Resources...',
userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { ListBucketsCommand } from '@aws-sdk/client-s3';

import {
type FindUserBucketsQueryHandler,
type FindUserBucketsQueryHandlerPayload,
type FindUserBucketsQueryHandlerResult,
} from './findUserBucketsQueryHandler.js';
import { type S3Client } from '../../../../../libs/s3/clients/s3Client/s3Client.js';
import { type UserBucketRepository } from '../../../domain/repositories/userBucketRepository/userBucketRepository.js';

export class FindUserBucketsQueryHandlerImpl implements FindUserBucketsQueryHandler {
public constructor(private readonly userBucketRepository: UserBucketRepository) {}
public constructor(
private readonly userBucketRepository: UserBucketRepository,
private readonly s3Client: S3Client,
) {}

public async execute(payload: FindUserBucketsQueryHandlerPayload): Promise<FindUserBucketsQueryHandlerResult> {
const { userId } = payload;

const existingBuckets = await this.s3Client.send(new ListBucketsCommand({}));

const userBuckets = await this.userBucketRepository.findUserBuckets({ userId });

const buckets = userBuckets.map((bucket) => ({
name: bucket.getBucketName(),
}));
const buckets = userBuckets
.map((bucket) => ({
name: bucket.getBucketName(),
}))
.filter((bucket) => existingBuckets.Buckets?.some((b) => b.Name === bucket.name));

return { buckets };
}
Expand Down
7 changes: 6 additions & 1 deletion apps/backend/src/modules/userModule/userModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { coreSymbols } from '../../core/symbols.js';
import { type DependencyInjectionContainer } from '../../libs/dependencyInjection/dependencyInjectionContainer.js';
import { type DependencyInjectionModule } from '../../libs/dependencyInjection/dependencyInjectionModule.js';
import { type LoggerService } from '../../libs/logger/services/loggerService/loggerService.js';
import { type S3Client } from '../../libs/s3/clients/s3Client/s3Client.js';
import { type UuidService } from '../../libs/uuid/services/uuidService/uuidService.js';
import { type AccessControlService } from '../authModule/application/services/accessControlService/accessControlService.js';
import { type TokenService } from '../authModule/application/services/tokenService/tokenService.js';
Expand Down Expand Up @@ -189,7 +190,11 @@ export class UserModule implements DependencyInjectionModule {

container.bind<FindUserBucketsQueryHandler>(
symbols.findUserBucketsQueryHandler,
() => new FindUserBucketsQueryHandlerImpl(container.get<UserBucketRepository>(symbols.userBucketRepository)),
() =>
new FindUserBucketsQueryHandlerImpl(
container.get<UserBucketRepository>(symbols.userBucketRepository),
container.get<S3Client>(coreSymbols.s3Client),
),
);

container.bind<UserHttpController>(
Expand Down

0 comments on commit 016d173

Please sign in to comment.