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

Bug bash fixes #103

Merged
merged 11 commits into from
Oct 16, 2023
2 changes: 1 addition & 1 deletion packages/marketplace-models/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freshworks-jaya/marketplace-models",
"version": "0.1.23",
"version": "0.1.25",
"description": "Provides type definitions for models in marketplace product events",
"repository": "[email protected]:freshdesk/jaya-lib.git",
"main": "lib/index.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/marketplace-models/src/types/PayloadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export interface MessagePart {
collection?: {
sub_parts: MessagePart[];
};
email?: {
content: string;
subject: string;
};
image?: { url: string };
quick_reply_button?: {
custom_reply_text?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/rule-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freshworks-jaya/rule-engine",
"version": "0.17.23",
"version": "0.17.25-beta-02",
"description": "Provides methods to process rules in product events in marketplace app",
"repository": "[email protected]:freshdesk/jaya-lib.git",
"main": "lib/index.js",
Expand Down Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@freshworks-jaya/freshchat-api": "0.7.33-beta-09",
"@freshworks-jaya/kairos-api": "^0.1.5",
"@freshworks-jaya/marketplace-models": "0.1.23",
"@freshworks-jaya/marketplace-models": "0.1.25",
"@freshworks-jaya/utilities": "^1.0.0",
"@google-cloud/logging": "^9.3.1",
"axios": "^0.21.4",
Expand Down
13 changes: 12 additions & 1 deletion packages/rule-engine/src/ActionExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
LabelCategory,
LabelSubcategory,
} from '@freshworks-jaya/marketplace-models';
import { Action, Api, CustomPlaceholdersMap } from './models/rule';
import { Action, AnyJson, Api, CustomPlaceholdersMap } from './models/rule';
import { Integrations, RuleEngineOptions } from './models/rule-engine';
import ruleConfig from './RuleConfig';
import { isUsernameGenerated, PlaceholdersMap, capitalizeAll } from '@freshworks-jaya/utilities';
import { Utils } from './Utils';
import Freshchat from '@freshworks-jaya/freshchat-api';
import { AxiosResponse } from 'axios';
import { APITraceCodes } from './models/error-codes';
import { LogSeverity } from './services/GoogleCloudLogging';

export class ActionExecutor {
/**
Expand Down Expand Up @@ -206,6 +208,15 @@ export class ActionExecutor {
// Error while executing an action
// Queietly suppressing it so that next action can be executed
// So, doing nothing here
if (options.enableLogger) {
Utils.log(
productEventPayload,
integrations,
APITraceCodes.ACTION_HANDLER_ERROR,
err as AnyJson,
LogSeverity.NOTICE,
);
}
}
}

Expand Down
37 changes: 30 additions & 7 deletions packages/rule-engine/src/RuleProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Simple library to process the rules
import { Event } from '@freshworks-jaya/marketplace-models';
import { Event, ProductEventPayload } from '@freshworks-jaya/marketplace-models';
import { ProductEventData } from '@freshworks-jaya/marketplace-models';
import { Block, Condition, MatchType, Rule, Trigger, TriggerAction, TriggerActor } from './models/rule';
import { AnyJson, Block, Condition, MatchType, Rule, Trigger, TriggerAction, TriggerActor } from './models/rule';
import { Integrations, RuleEngineOptions } from './models/rule-engine';
import { Promise } from 'bluebird';
import ruleConfig from './RuleConfig';
import { Utils } from './Utils';
import { APITraceCodes } from './models/error-codes';
import { LogSeverity } from './services/GoogleCloudLogging';

export class RuleProcessor {
/**
Expand Down Expand Up @@ -71,9 +74,14 @@ export class RuleProcessor {
public static isTriggerActorMatch(actor: TriggerActor, productEventData: ProductEventData): boolean {
const triggerActorFunc = ruleConfig.triggerActors && ruleConfig.triggerActors[actor.type];

if (triggerActorFunc) {
return triggerActorFunc(productEventData, actor);
try {
if (triggerActorFunc) {
return triggerActorFunc(productEventData, actor);
}
} catch (error) {
throw new Error('Invalid trigger actor : ' + JSON.stringify(actor));
}

throw new Error('Invalid trigger actor');
}

Expand All @@ -83,9 +91,14 @@ export class RuleProcessor {
public static isTriggerActionMatch(action: TriggerAction, event: Event, productEventData: ProductEventData): boolean {
const triggerActionFunc = ruleConfig.triggerActions && ruleConfig.triggerActions[action.type];

if (triggerActionFunc) {
return triggerActionFunc(event, productEventData, action);
try {
if (triggerActionFunc) {
return triggerActionFunc(event, productEventData, action);
}
} catch (error) {
throw new Error('Invalid trigger action : ' + action.type);
}

throw new Error('Invalid trigger action');
}

Expand Down Expand Up @@ -196,7 +209,17 @@ export class RuleProcessor {
await this.isRuleMatching(event, productEventData, currentRule, integrations, options);
firstMatchingRule = currentRule;
break;
} catch (err) {}
} catch (err) {
if (options.enableLogger) {
Utils.log(
productEventData as unknown as ProductEventPayload,
integrations,
APITraceCodes.FIRST_MATCHING_RULE,
err as AnyJson,
LogSeverity.NOTICE,
);
}
}
}
}
return firstMatchingRule ? Promise.resolve(firstMatchingRule) : Promise.reject('no matching rule');
Expand Down
25 changes: 7 additions & 18 deletions packages/rule-engine/src/TimerRuleEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Kairos, { KairosSchedule, KairosScheduleOptions } from '@freshworks-jaya/
import { Event, ProductEventPayload, ProductEventData, ModelProperties } from '@freshworks-jaya/marketplace-models';
import { ActionExecutor } from './ActionExecutor';
import {
AnyJson,
Api,
CustomPlaceholdersMap,
Rule,
Expand Down Expand Up @@ -150,10 +151,7 @@ export class TimerRuleEngine {
integrations,
ErrorCodes.KairosError,
{
error: {
data: err?.response?.data,
responseHeaders: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.KairosBulkCreateSchedules,
jobIds: schedulesToCreate.map((schedule) => schedule.jobId),
},
Expand Down Expand Up @@ -200,10 +198,7 @@ export class TimerRuleEngine {
integrations,
ErrorCodes.KairosError,
{
error: {
data: err?.response?.data,
responseHeaders: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.KairosDeleteCompletedSchedule,
jobId: externalEventPayload.data?.jobId,
},
Expand Down Expand Up @@ -338,16 +333,13 @@ export class TimerRuleEngine {
);
return Promise.resolve();
},
(err) => {
(err: AnyJson) => {
Utils.log(
payload,
integrations,
ErrorCodes.KairosError,
{
error: {
data: err?.response?.data,
responseHeaders: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.KairosCreteScheduleToDelayInvalidation,
jobId: createScheduleJobId,
},
Expand All @@ -372,16 +364,13 @@ export class TimerRuleEngine {
);
return Promise.resolve();
},
(err) => {
(err: AnyJson) => {
Utils.log(
payload,
integrations,
ErrorCodes.KairosError,
{
error: {
data: err?.response?.data,
responseHeaders: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.KairosDeleteInvalidatedSchedules,
jobIds: jobsToDelete,
},
Expand Down
18 changes: 18 additions & 0 deletions packages/rule-engine/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ export class Utils {
return messageContent;
}

/**
* Gets a concatenated string of messageParts with type 'text'.
*/
public static getMessagePartsEmailContent(messageParts: MessagePart[]): string {
let messageContent = '';

if (messageParts && messageParts.length) {
messageContent = messageParts
.filter((messagePart) => messagePart.email)
.map((messagePart) => {
return messagePart.email && messagePart.email.content;
})
.join(' ');
}

return messageContent;
}

public static processHanldebars(value: string, placeholders: PlaceholdersMap): string {
let processedString = '';
let isError = false;
Expand Down
14 changes: 13 additions & 1 deletion packages/rule-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
KairosCredentials,
} from './models/rule-engine';

import { Api, CustomPlaceholdersMap, Rule } from './models/rule';
import { AnyJson, Api, CustomPlaceholdersMap, Rule } from './models/rule';
import { RulePlugin } from './models/plugin';
import { RuleProcessor } from './RuleProcessor';
import { ActionExecutor } from './ActionExecutor';
import { TimerRuleEngine } from './TimerRuleEngine';
import ruleConfig from './RuleConfig';
import recommendedPlugins from './recommended/index';
import { Utils } from './Utils';
import { ErrorCodes } from './models/error-codes';
import { LogSeverity } from './services/GoogleCloudLogging';

export * from './models/rule';
export * from './models/rule-engine';
Expand Down Expand Up @@ -82,6 +85,15 @@ export class RuleEngine {
);
}
} catch (err) {
Utils.log(
payload,
integrations,
ErrorCodes.FreshchatAction,
{
error: err as AnyJson,
},
LogSeverity.ALERT,
);
return Promise.reject(err);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/rule-engine/src/models/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export enum ErrorTypes {
}

export enum APITraceCodes {
ACTION_HANDLER_ERROR = 'ACTION_HANDLER_ERROR',
CREATE_SCHEDULE_SUCCESS = 'CREATE_SCHEDULE_SUCCESS',
DELAY_SCHEDULE_CREATION = 'DELAY_SCHEDULE_CREATION',
EXECUTE_SCHEDULE_SUCCESS = 'EXECUTE_SCHEDULE_SUCCESS',
FIRST_MATCHING_RULE = 'FIRST_MATCHING_RULE',
INVALIDATE_SCHEDULE_SUCCESS = 'INVALIDATE_SCHEDULE_SUCCESS',
}
1 change: 1 addition & 0 deletions packages/rule-engine/src/models/rule-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RequestProxy } from '@freshworks-jaya/marketplace-models/lib/services/r
import { GoogleCloudLoggingConfig } from '../services/GoogleCloudLogging';

export interface RuleEngineOptions {
enableLogger?: boolean; // this is to be enabled only for debugging, should be disabled afterwards
isSchedulerEnabled: boolean;
isUseStaticIP: boolean;
maxProductEventDelay: number;
Expand Down
7 changes: 7 additions & 0 deletions packages/rule-engine/src/models/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export enum ConditionKey {
CallType = 'CALL_TYPE',
Channel = 'CHANNEL',
ConversationProperty = 'CONVERSATION_PROPERTY',
EmailBody = 'EMAIL_BODY',
LabelCategoryName = 'LABEL_CATEGORY_NAME',
LabelSubcategoryName = 'LABEL_SUBCATEGORY_NAME',
MessageText = 'MESSAGE_TEXT',
Expand Down Expand Up @@ -176,6 +177,12 @@ export interface TriggerWebhookValue {
password: string;
username: string;
};
castArray?: [
{
key: string;
type: string;
},
];
content?: string;
contentType?: WebhookContentType;
customHeaders?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Freshchat from '@freshworks-jaya/freshchat-api';
import { Integrations, RuleEngineOptions } from '../../models/rule-engine';
import { PlaceholdersMap } from '@freshworks-jaya/utilities';
import { Utils } from '../../Utils';
import { Api } from '../../models/rule';
import { AnyJson, Api } from '../../models/rule';
import { ErrorCodes, ErrorTypes } from '../../models/error-codes';
import { LogSeverity } from '../../services/GoogleCloudLogging';

Expand Down Expand Up @@ -63,10 +63,7 @@ export default async (
integrations,
ErrorCodes.FreshchatAction,
{
error: {
data: err?.response?.data,
headers: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.FreshchatAssignAgent,
},
LogSeverity.ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Freshchat from '@freshworks-jaya/freshchat-api';
import { Integrations, RuleEngineOptions } from '../../models/rule-engine';
import { PlaceholdersMap } from '@freshworks-jaya/utilities';
import { Utils } from '../../Utils';
import { Api } from '../../models/rule';
import { AnyJson, Api } from '../../models/rule';
import { ErrorCodes, ErrorTypes } from '../../models/error-codes';
import { LogSeverity } from '../../services/GoogleCloudLogging';

Expand Down Expand Up @@ -56,10 +56,7 @@ export default async (
integrations,
ErrorCodes.FreshchatAction,
{
error: {
data: err?.response?.data,
headers: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.FreshchatAssignGroup,
},
LogSeverity.ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Freshchat, { User as FreshchatUser } from '@freshworks-jaya/freshchat-api
import { Utils as FreshchatUtils } from '@freshworks-jaya/freshchat-api/lib/Utils';
import { isUsernameGenerated } from '@freshworks-jaya/utilities';
import { Utils } from '../../Utils';
import { Api, WebhookRequestType } from '../../models/rule';
import { AnyJson, Api, WebhookRequestType } from '../../models/rule';
import { ErrorCodes } from '../../models/error-codes';
import Constants from '../Constants';

Expand Down Expand Up @@ -193,10 +193,7 @@ export default async (
);
} catch (err) {
Utils.log(productEventPayload, integrations, ErrorCodes.FreshdeskTicket, {
error: {
data: err?.response?.data,
headers: err?.response?.headers,
},
error: err as AnyJson,
});
return Promise.reject('Error creating freshdesk ticket');
}
Expand Down
7 changes: 2 additions & 5 deletions packages/rule-engine/src/recommended/actions/reopen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConversationStatus, ProductEventPayload } from '@freshworks-jaya/market
import Freshchat from '@freshworks-jaya/freshchat-api';
import { Integrations, RuleEngineOptions } from '../../models/rule-engine';
import { PlaceholdersMap } from '@freshworks-jaya/utilities';
import { Api } from '../../models/rule';
import { AnyJson, Api } from '../../models/rule';
import { Utils } from '../../Utils';
import { ErrorCodes, ErrorTypes } from '../../models/error-codes';
import { LogSeverity } from '../../services/GoogleCloudLogging';
Expand Down Expand Up @@ -30,10 +30,7 @@ export default async (
integrations,
ErrorCodes.FreshchatAction,
{
error: {
data: err?.response?.data,
headers: err?.response?.headers,
},
error: err as AnyJson,
errorType: ErrorTypes.FreshchatReopenConversation,
},
LogSeverity.ERROR,
Expand Down
Loading
Loading