Skip to content

Commit

Permalink
Merge pull request #109 from mbc-net/hotfix/set_ttl
Browse files Browse the repository at this point in the history
[core] fix error when setting ttl
  • Loading branch information
koichimurakami authored Jan 7, 2025
2 parents 6b92773 + b2a98a5 commit 1cb4027
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -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/*"]
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
91 changes: 91 additions & 0 deletions packages/core/src/commands/command.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createMock } from '@golevelup/ts-jest'
import { Test } from '@nestjs/testing'
import { ModuleMocker, MockFunctionMetadata } from 'jest-mock'

Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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>(CommandService)
ttlService = moduleRef.get<TtlService>(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()
})
})
})
13 changes: 3 additions & 10 deletions packages/core/src/commands/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions packages/sequence/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/task/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/ui-setting/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 1cb4027

Please sign in to comment.