-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d14639
commit 509c130
Showing
12 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ourceModule/api/httpControllers/adminResourceHttpController/schemas/createBucketSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { type Static, Type } from '@sinclair/typebox'; | ||
|
||
import type * as contracts from '@common/contracts'; | ||
|
||
import { type TypeExtends } from '../../../../../../common/types/schemaExtends.js'; | ||
|
||
export const createBucketBodyDTOSchema = Type.Object({ | ||
bucketName: Type.String({ minLength: 1 }), | ||
}); | ||
|
||
export type CreateBucketBodyDTO = TypeExtends<Static<typeof createBucketBodyDTOSchema>, contracts.CreateBucketBody>; | ||
|
||
export const createBucketResponseBodyDTOSchema = Type.Null(); | ||
|
||
export type CreateBucketResponseBodyDTO = Static<typeof createBucketResponseBodyDTOSchema>; |
7 changes: 7 additions & 0 deletions
7
...dule/application/commandHandlers/createBucketCommandHandler/createBucketCommandHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { type CommandHandler } from '../../../../../common/types/commandHandler.js'; | ||
|
||
export interface CreateBucketCommandHandlerPayload { | ||
readonly bucketName: string; | ||
} | ||
|
||
export type CreateBucketCommandHandler = CommandHandler<CreateBucketCommandHandlerPayload, void>; |
52 changes: 52 additions & 0 deletions
52
...andHandlers/createBucketCommandHandler/createBucketCommandHandlerImpl.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { beforeEach, afterEach, expect, it, describe } from 'vitest'; | ||
|
||
import { type CreateBucketCommandHandler } from './createBucketCommandHandler.js'; | ||
import { testSymbols } from '../../../../../../tests/container/symbols.js'; | ||
import { TestContainer } from '../../../../../../tests/container/testContainer.js'; | ||
import { OperationNotValidError } from '../../../../../common/errors/common/operationNotValidError.js'; | ||
import { symbols } from '../../../symbols.js'; | ||
import { type S3TestUtils } from '../../../tests/utils/s3TestUtils.js'; | ||
|
||
describe('CreateBucketCommandHandler', () => { | ||
let commandHandler: CreateBucketCommandHandler; | ||
|
||
let s3TestUtils: S3TestUtils; | ||
|
||
const bucketName1 = 'resources1'; | ||
|
||
const bucketName2 = 'resources2'; | ||
|
||
beforeEach(async () => { | ||
const container = TestContainer.create(); | ||
|
||
commandHandler = container.get<CreateBucketCommandHandler>(symbols.createBucketCommandHandler); | ||
|
||
s3TestUtils = container.get<S3TestUtils>(testSymbols.s3TestUtils); | ||
}); | ||
|
||
afterEach(async () => { | ||
await Promise.all([s3TestUtils.deleteBucket(bucketName1), s3TestUtils.deleteBucket(bucketName2)]); | ||
}); | ||
|
||
it('create a bucket', async () => { | ||
await commandHandler.execute({ bucketName: bucketName1 }); | ||
|
||
const createdBuckets = await s3TestUtils.getBuckets(); | ||
|
||
expect(createdBuckets.includes(bucketName1)).toBe(true); | ||
}); | ||
|
||
it('throws an error - when a bucket with the same name already exists', async () => { | ||
await s3TestUtils.createBucket(bucketName2); | ||
|
||
try { | ||
await commandHandler.execute({ bucketName: bucketName2 }); | ||
} catch (error) { | ||
expect(error instanceof OperationNotValidError); | ||
|
||
return; | ||
} | ||
|
||
expect.fail(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
.../application/commandHandlers/createBucketCommandHandler/createBucketCommandHandlerImpl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { CreateBucketCommand, ListBucketsCommand } from '@aws-sdk/client-s3'; | ||
|
||
import { | ||
type CreateBucketCommandHandler, | ||
type CreateBucketCommandHandlerPayload, | ||
} from './createBucketCommandHandler.js'; | ||
import { OperationNotValidError } from '../../../../../common/errors/common/operationNotValidError.js'; | ||
import { type LoggerService } from '../../../../../libs/logger/services/loggerService/loggerService.js'; | ||
import { type S3Client } from '../../../../../libs/s3/clients/s3Client/s3Client.js'; | ||
|
||
export class CreateBucketCommandHandlerImpl implements CreateBucketCommandHandler { | ||
public constructor( | ||
private readonly s3Client: S3Client, | ||
private readonly loggerService: LoggerService, | ||
) {} | ||
|
||
public async execute(payload: CreateBucketCommandHandlerPayload): Promise<void> { | ||
const { bucketName } = payload; | ||
|
||
const result = await this.s3Client.send(new ListBucketsCommand({})); | ||
|
||
if (result.Buckets?.find((bucket) => bucket.Name === bucketName)) { | ||
throw new OperationNotValidError({ | ||
reason: 'Bucket already exists.', | ||
bucketName, | ||
}); | ||
} | ||
|
||
this.loggerService.debug({ | ||
message: 'Creating Bucket...', | ||
bucketName, | ||
}); | ||
|
||
await this.s3Client.send(new CreateBucketCommand({ Bucket: bucketName })); | ||
|
||
this.loggerService.debug({ | ||
message: 'Bucket created.', | ||
bucketName, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface CreateBucketBody { | ||
readonly bucketName: string; | ||
} |