new(basic.gblib): NEW keywords: DATEADD and DATEDIFF added.

This commit is contained in:
Rodrigo Rodriguez 2021-08-15 10:13:36 -03:00
parent 6c6a3cc148
commit a011ec0a33
3 changed files with 167 additions and 123 deletions

View file

@ -160,10 +160,10 @@ export class DialogKeywords {
* Returns an object ready to get information about difference in several ways * Returns an object ready to get information about difference in several ways
* like years, months or days. * like years, months or days.
* *
* @example days = DATEDIFF date1, date2 * @example days = DATEDIFF date1, date2, mode
* *
*/ */
public dateDiff(date1, date2) { public dateDiff(date1, date2, mode) {
let dt1 = date1; let dt1 = date1;
let dt2 = date2; let dt2 = date2;
if (!(dt1 instanceof Date)) { if (!(dt1 instanceof Date)) {
@ -172,17 +172,25 @@ export class DialogKeywords {
if (!(dt2 instanceof Date)) { if (!(dt2 instanceof Date)) {
dt2 = new Date(dt2); dt2 = new Date(dt2);
} }
return new DateDiff(date1, date2); const diff = new DateDiff(date1, date2);
switch (mode) {
case 'year': return diff.years();
case 'month': return diff.months();
case 'week': return diff.weeks();
case 'day': return diff.days();
case 'hour': return diff.hours();
case 'minute': return diff.minutes();
}
} }
/** /**
* Returns specified date week day in format 'Mon'. * Returns specified date week day in format 'Mon'.
* *
* @example DATEADD date1, date2, mode, value * @example DATEADD date, "minute", 60
* *
* https://stackoverflow.com/a/1214753/18511 * https://stackoverflow.com/a/1214753/18511
*/ */
public dateAdd(date, units, mode) { public dateAdd(date, mode, units) {
let dateCopy = date; let dateCopy = date;
if (!(dateCopy instanceof Date)) { if (!(dateCopy instanceof Date)) {
dateCopy = new Date(dateCopy); dateCopy = new Date(dateCopy);
@ -224,7 +232,7 @@ export class DialogKeywords {
array = array.join(", "); array = array.join(", ");
return array; return array;
} }
/** /**
* Returns the specified time in format hh:dd. * Returns the specified time in format hh:dd.
@ -243,7 +251,7 @@ export class DialogKeywords {
return i; return i;
} }
return addZero(date.getHours()) + ':' + addZero(date.getMinutes()); return addZero(date.getHours()) + ':' + addZero(date.getMinutes());
} }
/** /**
* Returns current time in format hh:dd. * Returns current time in format hh:dd.
@ -265,7 +273,7 @@ export class DialogKeywords {
{ timeZone: process.env.DEFAULT_TIMEZONE })); { timeZone: process.env.DEFAULT_TIMEZONE }));
return now.getHours() + ':' + now.getMinutes(); return now.getHours() + ':' + now.getMinutes();
} }
/** /**
* Sends a file to a given mobile. * Sends a file to a given mobile.
@ -276,7 +284,7 @@ export class DialogKeywords {
public async sendFileTo(step, mobile, filename, caption) { public async sendFileTo(step, mobile, filename, caption) {
GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`); GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`);
return await this.internalSendFile(null, mobile, filename, caption); return await this.internalSendFile(null, mobile, filename, caption);
} }
/** /**
* Sends a file to the current user. * Sends a file to the current user.
@ -286,7 +294,7 @@ export class DialogKeywords {
*/ */
public async sendFile(step, filename, caption) { public async sendFile(step, filename, caption) {
return await this.internalSendFile(step, null, filename, caption); return await this.internalSendFile(step, null, filename, caption);
} }
/** /**
* Defines the current language of the bot conversation. * Defines the current language of the bot conversation.
@ -302,7 +310,7 @@ export class DialogKeywords {
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines the maximum lines to scan in spreedsheets. * Defines the maximum lines to scan in spreedsheets.
@ -315,7 +323,7 @@ export class DialogKeywords {
user.basicOptions.maxLines = count; user.basicOptions.maxLines = count;
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines translator behaviour. * Defines translator behaviour.
@ -328,21 +336,21 @@ export class DialogKeywords {
user.basicOptions.translatorOn = (on.trim() === "on"); user.basicOptions.translatorOn = (on.trim() === "on");
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Returns the name of the user acquired by WhatsApp API. * Returns the name of the user acquired by WhatsApp API.
*/ */
public async userName(step) { public async userName(step) {
return step ? step.context.activity.from.name : 'N/A'; return step ? step.context.activity.from.name : 'N/A';
} }
/** /**
* OBSOLETE. * OBSOLETE.
*/ */
public async getFrom(step) { public async getFrom(step) {
return step ? await this.userMobile(step) : 'N/A'; return step ? await this.userMobile(step) : 'N/A';
} }
/** /**
@ -363,7 +371,7 @@ export class DialogKeywords {
} else { } else {
return step.context.activity['mobile']; return step.context.activity['mobile'];
} }
} }
/** /**
* Shows the subject menu to the user * Shows the subject menu to the user
@ -373,7 +381,7 @@ export class DialogKeywords {
*/ */
public async showMenu(step) { public async showMenu(step) {
return await step.beginDialog('/menu'); return await step.beginDialog('/menu');
} }
/** /**
* Performs the transfer of the conversation to a human agent. * Performs the transfer of the conversation to a human agent.
@ -383,7 +391,7 @@ export class DialogKeywords {
*/ */
public async transfer(step) { public async transfer(step) {
return await step.beginDialog('/t'); return await step.beginDialog('/t');
} }
/** /**
* Hears something from user and put it in a variable * Hears something from user and put it in a variable
@ -405,7 +413,7 @@ export class DialogKeywords {
} else { } else {
await step.beginDialog('/hear', opts); await step.beginDialog('/hear', opts);
} }
} }
/** /**
* Talks to the user by using the specified text. * Talks to the user by using the specified text.
@ -413,7 +421,7 @@ export class DialogKeywords {
public async talk(step, text: string) { public async talk(step, text: string) {
await this.min.conversationalService['sendTextWithOptions'](this.min, step, text, await this.min.conversationalService['sendTextWithOptions'](this.min, step, text,
this.user.basicOptions.translatorOn, null); this.user.basicOptions.translatorOn, null);
} }
private static getChannel(step): string { private static getChannel(step): string {
if (!isNaN(step.context.activity['mobile'])) { if (!isNaN(step.context.activity['mobile'])) {
@ -424,7 +432,7 @@ export class DialogKeywords {
} }
return 'webchat'; return 'webchat';
} }
} }
/** /**
@ -453,5 +461,5 @@ export class DialogKeywords {
await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption); await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
} }
} }
} }

View file

@ -288,6 +288,14 @@ export class GBVMService extends GBService {
return `setLanguage (step, ${$3})\n`; return `setLanguage (step, ${$3})\n`;
}); });
code = code.replace(/(datediff)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `dateDiff (step, ${$3})\n`;
});
code = code.replace(/(dateadd)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `dateAdd (step, ${$3})\n`;
});
code = code.replace(/(set max lines)(\s*)(.*)/gi, ($0, $1, $2, $3) => { code = code.replace(/(set max lines)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `setMaxLines (step, ${$3})\n`; return `setMaxLines (step, ${$3})\n`;
}); });
@ -500,6 +508,12 @@ export class GBVMService extends GBService {
code = code.replace(/("[^"]*"|'[^']*')|\bsetLanguage\b/gi, ($0, $1) => { code = code.replace(/("[^"]*"|'[^']*')|\bsetLanguage\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.setLanguage' : $1; return $1 === undefined ? 'this.setLanguage' : $1;
}); });
code = code.replace(/("[^"]*"|'[^']*')|\bdateAdd\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.dateAdd' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\bdateDiff\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.dateDiff' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\bsetMaxLines\b/gi, ($0, $1) => { code = code.replace(/("[^"]*"|'[^']*')|\bsetMaxLines\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.setMaxLines' : $1; return $1 === undefined ? 'this.setMaxLines' : $1;
}); });

View file

@ -31,6 +31,7 @@
\*****************************************************************************/ \*****************************************************************************/
'use strict'; 'use strict';
import { GBDialogStep, GBLog, GBMinInstance } from 'botlib'; import { GBDialogStep, GBLog, GBMinInstance } from 'botlib';
import { GBConfigService } from 'packages/core.gbapp/services/GBConfigService';
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
import urlJoin = require('url-join'); import urlJoin = require('url-join');
@ -342,7 +343,15 @@ export class SystemKeywords {
return filter; return filter;
}; };
function isValidDate(date) { const contentLocale = this.min.core.getParam<string>(
this.min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);
function isValidDate(dt) {
let date = SystemKeywords.getDateFromLocaleString(dt, contentLocale);
if (!(date instanceof Date)) { if (!(date instanceof Date)) {
date = new Date(date); date = new Date(date);
} }
@ -378,7 +387,7 @@ export class SystemKeywords {
if (isValidHour(filter.value)) { if (isValidHour(filter.value)) {
filter.dataType = 'hourInterval'; filter.dataType = 'hourInterval';
} else if (isValidDate(filter.value)) { } else if (isValidDate(filter.value)) {
filter.value = new Date(filter.value); filter.value = SystemKeywords.getDateFromLocaleString(filter.value, contentLocale);
filter.dataType = 'date'; filter.dataType = 'date';
} else if (isValidNumber(filter.value)) { } else if (isValidNumber(filter.value)) {
filter.value = Number.parseInt(filter.value); filter.value = Number.parseInt(filter.value);
@ -454,7 +463,7 @@ export class SystemKeywords {
break; break;
case 'date': case 'date':
const resultDate = new Date(result); const resultDate = SystemKeywords.getDateFromLocaleString(result, contentLocale);
switch (filter.operator) { switch (filter.operator) {
case '=': case '=':
if (resultDate.getTime() == filter.value.getTime()) if (resultDate.getTime() == filter.value.getTime())
@ -506,6 +515,19 @@ export class SystemKeywords {
} }
} }
private static getDateFromLocaleString(date: any, contentLocale: any) {
const parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})$/gi.exec(date);
switch (contentLocale) {
case 'pt':
date = new Date(Number.parseInt(parts[2]), Number.parseInt(parts[1]), Number.parseInt(parts[3]), 0, 0, 0, 0);
break;
case 'en':
date = new Date(Number.parseInt(parts[1]), Number.parseInt(parts[2]), Number.parseInt(parts[3]), 0, 0, 0, 0);
break;
}
return date;
}
/** /**
* Creates a folder in the bot instance drive. * Creates a folder in the bot instance drive.
* *