From 9d0191023414f4e27e421ee2d3070893e1392b7c Mon Sep 17 00:00:00 2001 From: koichimurakami Date: Thu, 2 Jan 2025 14:30:00 +0000 Subject: [PATCH 1/2] v0.1.47-beta.0 --- lerna.json | 2 +- package-lock.json | 16 ++++++++-------- packages/cli/package.json | 2 +- packages/core/package.json | 2 +- packages/sequence/package.json | 4 ++-- packages/task/package.json | 4 ++-- packages/ui-setting/package.json | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lerna.json b/lerna.json index aae85dd..2f66da5 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "packages": ["packages/*"] } diff --git a/package-lock.json b/package-lock.json index cf6826e..618a173 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28266,7 +28266,7 @@ }, "packages/cli": { "name": "@mbc-cqrs-serverless/cli", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "license": "MIT", "dependencies": { "commander": "^11.1.0", @@ -28290,7 +28290,7 @@ }, "packages/core": { "name": "@mbc-cqrs-serverless/core", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "license": "MIT", "dependencies": { "@aws-crypto/sha256-js": "^5.2.0", @@ -28467,26 +28467,26 @@ }, "packages/sequence": { "name": "@mbc-cqrs-serverless/sequence", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } }, "packages/task": { "name": "@mbc-cqrs-serverless/task", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } }, "packages/ui-setting": { "name": "@mbc-cqrs-serverless/ui-setting", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } } } diff --git a/packages/cli/package.json b/packages/cli/package.json index 89e04a2..11981aa 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/cli", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "description": "a CLI to get started with MBC CQRS serverless framework", "keywords": [ "mbc", diff --git a/packages/core/package.json b/packages/core/package.json index 1073ffb..ef848e5 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/core", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "description": "CQRS and event base core", "keywords": [ "mbc", diff --git a/packages/sequence/package.json b/packages/sequence/package.json index 467e573..93d7a79 100644 --- a/packages/sequence/package.json +++ b/packages/sequence/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/sequence", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "description": "Generate increment sequence with time-rotation", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } } diff --git a/packages/task/package.json b/packages/task/package.json index ed095d8..f29e842 100644 --- a/packages/task/package.json +++ b/packages/task/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/task", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "description": "long-running task", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } } diff --git a/packages/ui-setting/package.json b/packages/ui-setting/package.json index 66a0465..8d69f8f 100644 --- a/packages/ui-setting/package.json +++ b/packages/ui-setting/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/ui-setting", - "version": "0.1.46-beta.0", + "version": "0.1.47-beta.0", "description": "Setting master data", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.46-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.47-beta.0" } } From b2a98a5f716a0838f1ddfe1c18353b8b96d592a1 Mon Sep 17 00:00:00 2001 From: Quan Nguyen Ba Date: Tue, 7 Jan 2025 09:08:12 +0700 Subject: [PATCH 2/2] fix and add test for update ttl func --- .../core/src/commands/command.service.spec.ts | 91 +++++++++++++++++++ packages/core/src/commands/command.service.ts | 13 +-- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/packages/core/src/commands/command.service.spec.ts b/packages/core/src/commands/command.service.spec.ts index 9b76a67..2213d11 100644 --- a/packages/core/src/commands/command.service.spec.ts +++ b/packages/core/src/commands/command.service.spec.ts @@ -1,3 +1,4 @@ +import { createMock } from '@golevelup/ts-jest' import { Test } from '@nestjs/testing' import { ModuleMocker, MockFunctionMetadata } from 'jest-mock' @@ -7,6 +8,17 @@ import { CommandService } from './command.service' import { DataService } from './data.service' import { addSortKeyVersion } from '../helpers/key' import { BadRequestException } from '@nestjs/common' +import { TtlService } from './ttl.service' +import { + DynamoDBClient, + GetItemCommand, + PutItemCommand, + UpdateItemCommand, +} from '@aws-sdk/client-dynamodb' +import { MODULE_OPTIONS_TOKEN } from './command.module-definition' +import { ConfigService } from '@nestjs/config' +import 'aws-sdk-client-mock-jest' +import { mockClient } from 'aws-sdk-client-mock' const moduleMocker = new ModuleMocker(global) @@ -23,6 +35,16 @@ function buildItem(key: DetailKey, version = 0, attributes = {}) { } } +const config = { + DYNAMODB_ENDPOINT: 'http://localhost:8000', + DYNAMODB_REGION: 'ap-northeast-1', + AWS_ACCESS_KEY_ID: 'local', + AWS_SECRET_ACCESS_KEY: 'local', + AWS_DEFAULT_REGION: 'ap-northeast-1', + NODE_ENV: 'local', + APP_NAME: 'suisss-recruit', +} + describe('CommandService', () => { let commandService: CommandService @@ -281,3 +303,72 @@ describe('CommandService', () => { }) }) }) + +describe('CommandService', () => { + let commandService: CommandService + let dynamoDbService: DynamoDbService + let ttlService: TtlService + const dynamoDBMock = mockClient(DynamoDBClient) + + beforeEach(async () => { + const moduleRef = await Test.createTestingModule({ + providers: [ + CommandService, + DynamoDbService, + TtlService, + { + provide: MODULE_OPTIONS_TOKEN, + useValue: { + tableName: 'master', + }, + }, + { + provide: ConfigService, + useValue: { + get: (key) => config[key], + }, + }, + ], + }) + .useMocker(createMock) + .compile() + + commandService = moduleRef.get(CommandService) + ttlService = moduleRef.get(TtlService) + }) + + describe('updateTtl', () => { + it('should update with default TTL (null)', async () => { + dynamoDBMock.on(GetItemCommand).resolves({ + Item: {}, + }) + dynamoDBMock.on(PutItemCommand).resolves({} as any) + jest.spyOn(ttlService, 'calculateTtl').mockResolvedValue(-1) + await commandService.updateTtl({ + pk: 'master', + sk: 'test_ttl@2', + }) + + expect(dynamoDBMock).toHaveReceivedCommandTimes(PutItemCommand, 1) + }) + + it('should not update ttl when version is less than 1', async () => { + dynamoDBMock.on(GetItemCommand).resolves({ + Item: {}, + }) + dynamoDBMock.on(UpdateItemCommand).resolves({} as any) + jest.spyOn(ttlService, 'calculateTtl').mockResolvedValue(null) + await commandService.updateTtl({ + pk: 'master', + sk: 'test_ttl@0', + }) + + expect(dynamoDBMock).toHaveReceivedCommandTimes(UpdateItemCommand, 0) + }) + + afterEach(() => { + jest.clearAllMocks() + dynamoDBMock.reset() + }) + }) +}) diff --git a/packages/core/src/commands/command.service.ts b/packages/core/src/commands/command.service.ts index 9f84139..783b0f7 100644 --- a/packages/core/src/commands/command.service.ts +++ b/packages/core/src/commands/command.service.ts @@ -484,16 +484,9 @@ export class CommandService implements OnModuleInit, ICommandService { getTenantCode(key.pk), ) + command.ttl = ttl + this.logger.debug('updateTtl::', command) - return await this.dynamoDbService.updateItem( - this.tableName, - { - pk: key.pk, - sk: previousSk, - }, - { - set: { ttl }, - }, - ) + return await this.dynamoDbService.putItem(this.tableName, command) } }