Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat((pin 262) crr dd): [sc-24352] Improve Human Readable ID #658

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/plugin/components/services/timestamp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class TimestampService {
},
today () {
return moment().hour(0).minute(0).second(0).millisecond(0)
},
getYear (dateInput = new Date()) {
return moment().format('yyyy')
}
}
} // defaultProvider
Expand All @@ -24,6 +27,10 @@ class TimestampService {
today () {
return this.timeProvider.today()
}

getYear () {
return this.timeProvider.getYear()
}
} // class TimestampService

module.exports = {
Expand Down
13 changes: 11 additions & 2 deletions lib/plugin/components/state-resources/timestamp/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@

module.exports = class Timestamp {
init (resourceConfig, env) {
this.query = resourceConfig.query
this.timestamp = env.bootedServices.timestamp
}

run (event, context) {
context.sendTaskSuccess(this.timestamp.now())
if (this.query === '$TODAY') {
return context.sendTaskSuccess(this.timestamp.today())
}

if (this.query === '$YEAR') {
return context.sendTaskSuccess(this.timestamp.getYear())
}

return context.sendTaskSuccess(this.timestamp.now())
} // run
} // GetRegistryKey
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Comment": "Blueprint to get timestamp",
"version": "1.0",
"StartAt": "Timestamp",
"States": {
"Timestamp": {
"Type": "Task",
"Resource": "module:timestamp",
"ResourceConfig": {
"query": "$TODAY"
},
"ResultPath": "$.timestamp",
"End": true
}
},
"restrictions": [
{
"roleId": "$authenticated",
"allows": [
"*"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Comment": "Blueprint to get timestamp",
"version": "1.0",
"StartAt": "Timestamp",
"States": {
"Timestamp": {
"Type": "Task",
"Resource": "module:timestamp",
"ResourceConfig": {
"query": "$YEAR"
},
"ResultPath": "$.timestamp",
"End": true
}
},
"restrictions": [
{
"roleId": "$authenticated",
"allows": [
"*"
]
}
]
}
2 changes: 1 addition & 1 deletion test/list-state-machines-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('list available state machines', () => {
it('available-state-machines state resource', async () => {
const stateMachines = await runAvailableStateMachines()

expect(stateMachines[0].name).to.eql('tymlyTest_timestamp_1_0')
expect(stateMachines[0].name).to.eql('tymlyTest_timestampNow_1_0')
expect(stateMachines[0].description).to.eql('Blueprint to get timestamp')
})

Expand Down
46 changes: 32 additions & 14 deletions test/timestamp-state-resource-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const expect = require('chai').expect
const tymly = require('../lib')
const moment = require('moment')

const TestTimestamp = moment([2000, 2, 28, 22, 35, 0]).local()
const TestTimestampString = `${TestTimestamp}`
const stateMachines = {
now: 'tymlyTest_timestampNow_1_0',
today: 'tymlyTest_timestampToday_1_0',
year: 'tymlyTest_timestampYear_1_0'
}

const timestampStateMachine = 'tymlyTest_timestamp_1_0'
const todayCheck = moment().hour(0).minute(0).second(0).millisecond(0)
const yearCheck = moment().format('yyyy')

describe('Timestamp state resources', function () {
this.timeout(process.env.TIMEOUT || 5000)
Expand All @@ -31,25 +35,39 @@ describe('Timestamp state resources', function () {
tymlyService = tymlyServices.tymly
statebox = tymlyServices.statebox

tymlyServices.timestamp.timeProvider = {
now () {
return TestTimestamp
}
} // debug provider

done()
}
)
})

it('run the state machine to get a timestamp', async () => {
it('run the state machine to get a timestamp for now', async () => {
const execDesc = await statebox.startExecution(
{},
stateMachines.now,
{ sendResponse: 'COMPLETE' }
)

expect(execDesc.ctx.timestamp).to.not.eql(null)
})

it('run the state machine to get a timestamp for today', async () => {
const execDesc = await statebox.startExecution(
{ query: '$TODAY' },
stateMachines.today,
{ sendResponse: 'COMPLETE' }
)

expect(execDesc.ctx.timestamp.format()).to.eql(todayCheck.format())
})

it('run the state machine to get a timestamp for the year', async () => {
const execDesc = await statebox.startExecution(
{ },
timestampStateMachine,
{ query: '$YEAR' },
stateMachines.year,
{ sendResponse: 'COMPLETE' }
)
expect(execDesc.ctx.timestamp).to.eql(TestTimestamp)
expect(execDesc.ctx.timestamp.toString()).to.eql(TestTimestampString)

expect(execDesc.ctx.timestamp).to.eql(yearCheck)
})

it('shutdown Tymly', async () => {
Expand Down