diff --git a/lib/plugin/components/state-resources/timestamp-format/doc/example.json b/lib/plugin/components/state-resources/timestamp-format/doc/example.json new file mode 100644 index 00000000..7a75c622 --- /dev/null +++ b/lib/plugin/components/state-resources/timestamp-format/doc/example.json @@ -0,0 +1,12 @@ +{ + "TimestampFormat": { + "Type": "Task", + "Parameters": { + "timestamp.$": "$.timestamp", + "format": "DD/MM/YYYY" + }, + "Resource": "module:timestampFormat", + "ResultPath": "$.timestamp", + "End": true + } +} diff --git a/lib/plugin/components/state-resources/timestamp-format/doc/index.js b/lib/plugin/components/state-resources/timestamp-format/doc/index.js new file mode 100644 index 00000000..37553727 --- /dev/null +++ b/lib/plugin/components/state-resources/timestamp-format/doc/index.js @@ -0,0 +1,4 @@ +module.exports = { + description: 'Generates a formatted timestamp', + example: require('./example.json') +} diff --git a/lib/plugin/components/state-resources/timestamp-format/index.js b/lib/plugin/components/state-resources/timestamp-format/index.js new file mode 100644 index 00000000..c24b7774 --- /dev/null +++ b/lib/plugin/components/state-resources/timestamp-format/index.js @@ -0,0 +1,11 @@ +module.exports = class TimestampFormat { + init (resourceConfig, env) { + this.timestamp = env.bootedServices.timestamp + } + + run (event, context) { + return context.sendTaskSuccess( + this.timestamp.format(event.timestamp, event.format) + ) + } +} diff --git a/test/fixtures/blueprints/timestamp-blueprint/state-machines/timestamp-format.json b/test/fixtures/blueprints/timestamp-blueprint/state-machines/timestamp-format.json new file mode 100644 index 00000000..f3ccb129 --- /dev/null +++ b/test/fixtures/blueprints/timestamp-blueprint/state-machines/timestamp-format.json @@ -0,0 +1,21 @@ +{ + "Comment": "Blueprint to get a formatted timestamp", + "version": "1.0", + "StartAt": "Timestamp", + "States": { + "Timestamp": { + "Type": "Task", + "Resource": "module:timestampFormat", + "ResultPath": "$.timestamp", + "End": true + } + }, + "restrictions": [ + { + "roleId": "$authenticated", + "allows": [ + "*" + ] + } + ] +} diff --git a/test/timestamp-state-resource-tests.js b/test/timestamp-state-resource-tests.js index 3a44d6de..ca499396 100644 --- a/test/timestamp-state-resource-tests.js +++ b/test/timestamp-state-resource-tests.js @@ -8,7 +8,8 @@ const moment = require('moment') const stateMachines = { now: 'tymlyTest_timestampNow_1_0', today: 'tymlyTest_timestampToday_1_0', - year: 'tymlyTest_timestampYear_1_0' + year: 'tymlyTest_timestampYear_1_0', + format: 'tymlyTest_timestampFormat_1_0' } const todayCheck = moment().hour(0).minute(0).second(0).millisecond(0) @@ -70,6 +71,18 @@ describe('Timestamp state resources', function () { expect(execDesc.ctx.timestamp).to.eql(yearCheck) }) + it('run the state machine to get a formatted timestamp', async () => { + const date = moment().date(1).month(0).year(2020) + + const execDesc = await statebox.startExecution( + { timestamp: date, format: 'DD/MM/YY' }, + stateMachines.format, + { sendResponse: 'COMPLETE' } + ) + + expect(execDesc.ctx.timestamp).to.eql('01/01/20') + }) + it('shutdown Tymly', async () => { await tymlyService.shutdown() })