Skip to content

Commit

Permalink
Merge branch 'features/whatsapp-parameters-PRE' into master-pre
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele Panico committed Nov 11, 2024
2 parents f4a0a06 + ca54e23 commit 9e62a9f
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,65 @@
</cds-select>
</div>

<div *ngIf="selected_template" class="receiver-list">

<!-- <cds-whatsapp-receiver *ngIf="receiver_list.length == 0" [template]="selected_template"
[receiver]="receiver_list[0]" [index]=0 (receiverValue)="onReceiverEmitted($event, 0)"
(deleteReceiver)="onReceiverDeleteEmitted($event)">
</cds-whatsapp-receiver>
-->


<div *ngIf="action.payload.receiver_list.length > 0">
<div *ngFor="let rcv of action.payload.receiver_list; let id = index">
<cds-whatsapp-receiver
[template]="selected_template"
[receiver]="rcv"
[index]=id
(receiverValue)="onReceiverEmitted($event, id)">
</cds-whatsapp-receiver>
</div>
</div>

</div>

<hr class="custom-divider">

<div class="field-box">
<div class="condition-container">
<div class="icon-action">
<img class="active-icon" src="assets/images/icons/if_condition.svg">
</div>
<span class="condition-text">{{'CDSCanvas.Success' | translate }}</span>
</div>
<cds-select id="text-condition"
[items]="listOfIntents"
[bindLabelSelect]="'name'"
[bindValueSelect]="'value'"
[itemSelected]="action?.trueIntent"
[placeholder]="'CDSCanvas.SelectABlock' | translate"
[clearable]="true"
(onSelected)="onChangeBlockSelect($event, 'trueIntent')"
(onReset)="onResetBlockSelect($event, 'trueIntent')">
</cds-select>
</div>

<div class="field-box">
<div class="condition-container">
<span class="condition-text">{{'CDSCanvas.Else' | translate | titlecase }}</span>
</div>
<cds-select id="text-condition"
[items]="listOfIntents"
[bindLabelSelect]="'name'"
[bindValueSelect]="'value'"
[itemSelected]="action?.falseIntent"
[placeholder]="'CDSCanvas.SelectABlock' | translate"
[clearable]="true"
(onSelected)="onChangeBlockSelect($event, 'falseIntent')"
(onReset)="onResetBlockSelect($event, 'falseIntent')">
</cds-select>
</div>

</div>

<div *ngIf="showLoader" class="loader-container">
Expand Down Expand Up @@ -111,26 +170,6 @@
target="_blank">{{'CDSCanvas.CreateOneNow' | translate}}</a></p>
</div>

<div *ngIf="selected_template" class="receiver-list">

<!-- <cds-whatsapp-receiver *ngIf="receiver_list.length == 0" [template]="selected_template"
[receiver]="receiver_list[0]" [index]=0 (receiverValue)="onReceiverEmitted($event, 0)"
(deleteReceiver)="onReceiverDeleteEmitted($event)">
</cds-whatsapp-receiver>
-->


<div *ngIf="action.payload.receiver_list.length > 0">
<div *ngFor="let rcv of action.payload.receiver_list; let id = index">
<cds-whatsapp-receiver
[template]="selected_template"
[receiver]="rcv"
[index]=id
(receiverValue)="onReceiverEmitted($event, id)">
</cds-whatsapp-receiver>
</div>
</div>

</div>


</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Component, ElementRef, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Subscription } from 'rxjs';
import { IntentService } from 'src/app/chatbot-design-studio/services/intent.service';
import { TYPE_UPDATE_ACTION } from 'src/app/chatbot-design-studio/utils';
import { ActionSendWhatsapp } from 'src/app/models/action-model';
import { Intent } from 'src/app/models/intent-model';
import { DashboardService } from 'src/app/services/dashboard.service';
Expand All @@ -17,7 +20,19 @@ export class CdsActionSendWhatsappComponent implements OnInit {
@Input() intentSelected: Intent;
@Input() previewMode: boolean = true;
@Output() updateAndSaveAction = new EventEmitter();

@Output() onConnectorChange = new EventEmitter<{type: 'create' | 'delete', fromId: string, toId: string}>()

listOfIntents: Array<{name: string, value: string, icon?:string}>;

// Connectors
idIntentSelected: string;
idConnectorTrue: string;
idConnectorFalse: string;
isConnectedTrue: boolean = false;
isConnectedFalse: boolean = false;
connector: any;
private subscriptionChangedConnector: Subscription;

project_id: string;

templates_list = [];
Expand All @@ -32,18 +47,97 @@ export class CdsActionSendWhatsappComponent implements OnInit {

constructor(
private dashboardService: DashboardService,
private intentService: IntentService,
private whatsapp: WhatsappService,
public el: ElementRef
) { }

ngOnInit(): void {
this.logger.debug("[ACTION-SEND WHATSAPP] action detail: ", this.action);
this.subscriptionChangedConnector = this.intentService.isChangedConnector$.subscribe((connector: any) => {
this.logger.debug('[ACTION-SEND WHATSAPP] isChangedConnector -->', connector);
this.connector = connector;
this.updateConnector();
});
this.initialize();
}

/** */
ngOnDestroy() {
if (this.subscriptionChangedConnector) {
this.subscriptionChangedConnector.unsubscribe();
}
}

private checkConnectionStatus(){
if(this.action.trueIntent){
this.isConnectedTrue = true;
} else {
this.isConnectedTrue = false;
}
if(this.action.falseIntent){
this.isConnectedFalse = true;
} else {
this.isConnectedFalse = false;
}
}

initializeConnector() {
this.idIntentSelected = this.intentSelected.intent_id;
this.idConnectorTrue = this.idIntentSelected+'/'+this.action._tdActionId + '/true';
this.idConnectorFalse = this.idIntentSelected+'/'+this.action._tdActionId + '/false';
this.listOfIntents = this.intentService.getListOfIntents();
this.checkConnectionStatus();
}

private updateConnector(){
try {
const array = this.connector.fromId.split("/");
const idAction= array[1];
if(idAction === this.action._tdActionId){
if(this.connector.deleted){
if(array[array.length -1] === 'true'){
this.action.trueIntent = null
this.isConnectedTrue = false
}
if(array[array.length -1] === 'false'){
this.action.falseIntent = null
this.isConnectedFalse = false;
}
if(this.connector.save)this.updateAndSaveAction.emit({type: TYPE_UPDATE_ACTION.CONNECTOR, element: this.connector});
} else {
this.logger.debug('[ACTION-SEND WHATSAPP] updateConnector', this.connector.toId, this.connector.fromId ,this.action, array[array.length-1]);
if(array[array.length -1] === 'true'){
this.isConnectedTrue = true;
this.action.trueIntent = '#'+this.connector.toId;
if(this.connector.save)this.updateAndSaveAction.emit({type: TYPE_UPDATE_ACTION.CONNECTOR, element: this.connector});
}
if(array[array.length -1] === 'false'){
this.isConnectedFalse = true;
if(this.action.falseIntent !== '#'+this.connector.toId){
this.action.falseIntent = '#'+this.connector.toId;
if(this.connector.save)this.updateAndSaveAction.emit({type: TYPE_UPDATE_ACTION.CONNECTOR, element: this.connector});
}
}
}

}
} catch (error) {
this.logger.error('[ACTION-SEND WHATSAPP] updateConnector error: ', error);
}
}

private initialize(){
this.project_id = this.dashboardService.projectID
this.action.payload.id_project = this.project_id
if (this.previewMode == false) {
this.logger.log("Whatsapp static project_id: ", this.project_id);
this.showLoader = true;
this.getTemplates();
}
if(this.intentSelected){
this.initializeConnector();
}
}

getTemplates() {
Expand All @@ -62,9 +156,9 @@ export class CdsActionSendWhatsappComponent implements OnInit {

}, error: (error) => {
this.showLoader = false;
this.logger.log("[SEND WHATSAPP] error get templates: ", error);
this.logger.log("[ACTION-SEND WHATSAPP] error get templates: ", error);
}, complete: () => {
this.logger.log("[SEND WHATSAPP] get templates completed: ");
this.logger.log("[ACTION-SEND WHATSAPP] get templates completed: ");
if (this.action.templateName) {
this.selected_template = this.templates_list.find(t => t.name === this.action.templateName);
}
Expand All @@ -73,7 +167,7 @@ export class CdsActionSendWhatsappComponent implements OnInit {
}

onChangeSelect(event) {
this.logger.debug("[ACTION SEND WHATSAPP] onChangeSelect event", event);
this.logger.debug("[ACTION-SEND WHATSAPP] onChangeSelect event", event);
this.selected_template = event;
this.action.templateName = this.selected_template.name;
this.action.payload.template.name = this.selected_template.name;
Expand All @@ -100,14 +194,43 @@ export class CdsActionSendWhatsappComponent implements OnInit {
this.updateAndSaveAction.emit();
}

this.logger.debug("[ACTION WHATSAPP] Action updated ", this.action.payload);
this.logger.debug("[ACTION-SEND WHATSAPP] Action updated ", this.action.payload);
}
}

onChangeBlockSelect(event:{name: string, value: string}, type: 'trueIntent' | 'falseIntent') {
if(event){
this.action[type]=event.value

switch(type){
case 'trueIntent':
this.onConnectorChange.emit({ type: 'create', fromId: this.idConnectorTrue, toId: this.action.trueIntent})
break;
case 'falseIntent':
this.onConnectorChange.emit({ type: 'create', fromId: this.idConnectorFalse, toId: this.action.falseIntent})
break;
}
this.updateAndSaveAction.emit({type: TYPE_UPDATE_ACTION.ACTION, element: this.action});
}
}

onResetBlockSelect(event:{name: string, value: string}, type: 'trueIntent' | 'falseIntent') {
switch(type){
case 'trueIntent':
this.onConnectorChange.emit({ type: 'delete', fromId: this.idConnectorTrue, toId: this.action.trueIntent})
break;
case 'falseIntent':
this.onConnectorChange.emit({ type: 'delete', fromId: this.idConnectorFalse, toId: this.action.falseIntent})
break;
}
this.action[type] = null;
this.updateAndSaveAction.emit({type: TYPE_UPDATE_ACTION.ACTION, element: this.action});
}

onReceiverEmitted(event, index) {
// update receiver
this.action.payload.receiver_list[index] = event;
this.logger.log("[ACTION WHATSAPP] Action updated ", this.action.payload);
this.logger.log("[ACTION-SEND WHATSAPP] Action updated ", this.action.payload);
this.updateAndSaveAction.emit()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,10 @@

<cds-action-send-whatsapp *ngSwitchCase="TYPE_ACTION.SEND_WHATSAPP"
class="panel-response"
[intentSelected]="intentSelected"
[action]="elementSelected"
[intentName]="intentSelected.intent_display_name"
[previewMode]="false"
(onConnectorChange)="onConnectorChange($event.type, $event.fromId, $event.toId)"
(updateAndSaveAction)="onSaveIntent($event)">
</cds-action-send-whatsapp>

Expand Down
28 changes: 28 additions & 0 deletions src/app/chatbot-design-studio/services/connector.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,34 @@ export class ConnectorService {
}
}

/** SEND WHATSAPP */
if(action._tdActionType === TYPE_ACTION.SEND_WHATSAPP){
if(action.trueIntent && action.trueIntent !== ''){
idConnectorFrom = intent.intent_id+'/'+action._tdActionId + '/true';
idConnectorTo = action.trueIntent.replace("#", "");
if(!this.intentExists(idConnectorTo)){
action.trueIntent = '';
idConnectorTo = null;
}
this.logger.log('[CONNECTOR-SERV] - ONLINE_AGENTS ACTION -> idConnectorFrom', idConnectorFrom);
this.logger.log('[CONNECTOR-SERV] - ONLINE_AGENTS ACTION -> idConnectorTo', idConnectorTo);
// this.createConnectorFromId(idConnectorFrom, idConnectorTo);
this.createConnector(intent, idConnectorFrom, idConnectorTo);
}
if(action.falseIntent && action.falseIntent !== ''){
idConnectorFrom = intent.intent_id+'/'+action._tdActionId + '/false';
idConnectorTo = action.falseIntent.replace("#", "");
if(!this.intentExists(idConnectorTo)){
action.falseIntent = '';
idConnectorTo = null;
}
this.logger.log('[CONNECTOR-SERV] - ONLINE_AGENTS ACTION -> idConnectorFrom', idConnectorFrom);
this.logger.log('[CONNECTOR-SERV] - ONLINE_AGENTS ACTION -> idConnectorTo', idConnectorTo);
// this.createConnectorFromId(idConnectorFrom, idConnectorTo);
this.createConnector(intent, idConnectorFrom, idConnectorTo);
}
}


});
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/chatbot-design-studio/services/intent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,8 @@ export class IntentService {

if(typeAction === TYPE_ACTION.SEND_WHATSAPP){
action = new ActionSendWhatsapp();
action.payload = new WhatsappBroadcast();
action.payload = new WhatsappBroadcast();
(action.payload as WhatsappBroadcast).id_project = this.intentSelected.id_project
}
return action;
}
Expand Down

0 comments on commit 9e62a9f

Please sign in to comment.