Skip to content

Commit 984bb79

Browse files
committed
fix: expose currentTime and utcOffset to template logic in trigger() and init()
Signed-off-by: Supritha Rajashekar <supritharajashekar10@gmail.com>
1 parent d25a21a commit 984bb79

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/TemplateArchiveProcessor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class TemplateArchiveProcessor {
8787
* @param {object} request - the request to send to the template logic
8888
* @param {object} state - the current state of the template
8989
* @param {[string]} currentTime - the current time, defaults to now
90-
* @param {[number]} utcOffset - the UTC offer, defaults to zero
90+
* @param {[number]} utcOffset - the UTC offset, defaults to zero
9191
* @returns {Promise} the response and any events
9292
*/
9393
async trigger(data: any, request: any, state?: any, currentTime?: string, utcOffset?: number): Promise<TriggerResponse> {
@@ -112,14 +112,16 @@ export class TemplateArchiveProcessor {
112112
compiledCode[tsFile.getIdentifier()] = result;
113113
}
114114
// console.log(compiledCode['logic/logic.ts'].code);
115+
const resolvedTime = currentTime ?? new Date().toISOString();
116+
const resolvedOffset = utcOffset ?? 0;
115117
const evaluator = new JavaScriptEvaluator();
116118
const evalResponse = await evaluator.evalDangerously( {
117119
templateLogic: true,
118120
verbose: false,
119121
functionName: 'trigger',
120122
code: compiledCode['logic/logic.ts'].code, // TODO DCS - how to find the code to run?
121123
argumentNames: ['data', 'request', 'state'],
122-
arguments: [data, request, state, currentTime, utcOffset]
124+
arguments: [data, request, state, resolvedTime, resolvedOffset]
123125
});
124126
if(evalResponse.result) {
125127
return evalResponse.result;
@@ -136,8 +138,8 @@ export class TemplateArchiveProcessor {
136138
/**
137139
* Init the logic of a template
138140
* @param {[string]} currentTime - the current time, defaults to now
139-
* @param {[number]} utcOffset - the UTC offer, defaults to zero
140-
* @returns {Promise} the response and any events
141+
* @param {[number]} utcOffset - the UTC offset, defaults to zero
142+
* @returns {Promise<InitResponse>} the new state
141143
*/
142144
async init(data: any, currentTime?: string, utcOffset?: number): Promise<InitResponse> {
143145
const logicManager = this.template.getLogicManager();
@@ -161,14 +163,16 @@ export class TemplateArchiveProcessor {
161163
compiledCode[tsFile.getIdentifier()] = result;
162164
}
163165
// console.log(compiledCode['logic/logic.ts'].code);
166+
const resolvedTime = currentTime ?? new Date().toISOString();
167+
const resolvedOffset = utcOffset ?? 0;
164168
const evaluator = new JavaScriptEvaluator();
165169
const evalResponse = await evaluator.evalDangerously( {
166170
templateLogic: true,
167171
verbose: false,
168172
functionName: 'init',
169173
code: compiledCode['logic/logic.ts'].code, // TODO DCS - how to find the code to run?
170174
argumentNames: ['data'],
171-
arguments: [data, currentTime, utcOffset]
175+
arguments: [data, resolvedTime, resolvedOffset]
172176
});
173177
if(evalResponse.result) {
174178
return evalResponse.result;

0 commit comments

Comments
 (0)