Skip to content

Commit

Permalink
Merge pull request #309 from plivo/SMS-6065
Browse files Browse the repository at this point in the history
SMS:6065- Node SDK for Verify
  • Loading branch information
narayana-plivo authored Aug 10, 2023
2 parents d6d9ff3 + fc5f720 commit 826785e
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Change Log

## [v4.55.0](https://github.com/plivo/plivo-node/tree/v4.55.0) (2023-08-10)
**Verify Service API's**
- Create Session API - To initiate a session
- Get Session API - Get session detail
- List Session API - List Session details
- Validate Session API

## [v4.54.0](https://github.com/plivo/plivo-node/tree/v4.54.0) (2023-08-07)
**Feature - WhatsApp message support**
- Added new param `template` and new message_type `whatsapp` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message)
- Added new `message_states` (`read`) `message_type`(`whatsapp`),`conversation_id`, `conversation_origin`, `conversation_expiry_timestamp` in [list all messages API](https://www.plivo.com/docs/sms/api/message#list-all-messages) and [get message details API](https://www.plivo.com/docs/sms/api/message#retrieve-a-message) response


## [4.53.0](https://github.com/plivo/plivo-node/tree/v4.53.0) (2023-08-03)
**Feature - DLT parameters**
- Added new params `DLTEntityID`, `DLTTemplateID`, `DLTTemplateCategory` to the [send message API](https://www.plivo.com/docs/sms/api/message/send-a-message/)
Expand Down
23 changes: 23 additions & 0 deletions examples/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let plivo = require('plivo')

let client = new plivo.Client(process.env.PLIVO_AUTH_ID, process.env.PLIVO_AUTH_TOKEN);

client.verify_session.create({
recipient: '+14156667778',
}).then(function(response) {
console.log(response)
});

client.verify_session.get('5c533d15-1975-4f20-80be-40174142ea37').then(function(response) {
console.log(response)
});

client.verify_session.list().then(function(response) {
console.log(response)
});

client.verify_session.validate({id:'c549be00-0e74-4ccb-ac04-8a778312b861',otp:'123456'}).then(function(response) {
console.log(response)
}).catch(function (error) {
console.log(error)
});
313 changes: 313 additions & 0 deletions lib/resources/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
import * as _ from "lodash";

import {
PlivoResource,
PlivoResourceInterface
} from '../base';
import {
extend,
validate
} from '../utils/common.js';

const action = 'Verify/Session/';
const idField = 'session_uuid';
let actionKey = Symbol('api action');
let klassKey = Symbol('constructor');
let idKey = Symbol('id filed');
let clientKey = Symbol('make api call');


export class SessionResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
this.sessionUuid = params.sessionUuid;
if (params.invalidNumber != undefined ){
this.invalid_number = params.invalidNumber;
}
}
}

export class ValidateSessionResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
if (params.invalidNumber != undefined ){
this.invalid_number = params.invalidNumber;
}
}
}

export class SessionGetResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.sessionUuid = params.sessionUuid;
this.appUuid = params.appUuid;
this.alias = params.alias;
this.recipient = params.recipient;
this.channel = params.channel;
this.status = params.status;
this.count = params.count;
this.requestor_ip = params.requestorIp;
this.destination_country_iso2 = params.destinationCountryIso2;
this.destination_network = params.destinationNetwork;
this.attemptDetails = params.attemptDetails;
this.createdAt = params.createdAt;
this.updatedAt = params.updatedAt;

this.charges = Object.assign({}, params.charges);

this.charges.totalCharge = params.charges.totalCharge;
this.charges.validationCharge = params.charges.validationCharge;
this.charges.attemptCharges = params.charges.attemptCharges
}
}

export class SessionListResponse {
constructor(params) {
params = params || {};
this.sessionUuid = params.sessionUuid;
this.appUuid = params.appUuid;
this.recipient = params.recipient;
this.alias = params.alias;
this.channel = params.channel;
this.status = params.status;
this.count = params.count;
this.requestor_ip = params.requestorIp;
this.destination_country_iso2 = params.destinationCountryIso2;
this.destination_network = params.destinationNetwork;
this.attemptDetails = params.attemptDetails;
this.createdAt = params.createdAt;
this.updatedAt = params.updatedAt;

this.charges = Object.assign({}, params.charges);

this.charges.totalCharge = params.charges.totalCharge;
this.charges.validationCharge = params.charges.validationCharge;
this.charges.attemptCharges = params.charges.attemptCharges
}
}

/**
* Represents a Session
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
export class Session extends PlivoResource {
constructor(client, data = {}) {
super(action, Session, idField, client);
this[actionKey] = action;
this[clientKey] = client;
if (idField in data) {
this.id = data[idField];
};

extend(this, data);
}

}


/**
* Represents a Session Interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
export class SessionInterface extends PlivoResourceInterface {
constructor(client, data = {}) {
super(action, Session, idField, client);
extend(this, data);
this[clientKey] = client;
this[actionKey] = action;
this[klassKey] = Session;
this[idKey] = idField;
}

/**
* Send Session
* @method
* @param {Object} sessionReq- request body fields
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
create(sessionReq){

var isObject = arguments.length;
var app_uuid, recipient, url, method, channel

if (isObject === 1) {
app_uuid = sessionReq.app_uuid
recipient = sessionReq.recipient
url = sessionReq.url
method = sessionReq.method
channel = sessionReq.channel
}

let errors = validate([{
field: 'recipient',
value: recipient,
validators: ['isRequired']
},
]);

if (errors) {
return errors
}

let params = {}
if (isObject === 1) {
if(app_uuid) {
params.app_uuid = app_uuid
}
if(recipient) {
params.recipient = recipient
}
if(channel) {
params.channel = channel
}
if(url) {
params.url = url
}
if(method) {
params.method = method
}
}

let client = this[clientKey];
let idField = this[idKey];
let action = this[actionKey] + (this.id ? this.id + '/' : '');

return new Promise((resolve, reject) => {
client('POST', action, params)
.then(response => {
resolve(new SessionResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
})

}


/**
* Get Session by given id
* @method
* @param {string} id - id of session
* @promise {object} return {@link Session} object if success
* @fail {Error} return Error
*/
get(id) {
let errors = validate([{
field: 'id',
value: id,
validators: ['isRequired']
}]);

if (errors) {
return errors;
}

let client = this[clientKey];
let action = this[actionKey];

return new Promise((resolve, reject) => {
if (action !== '' && !id) {
reject(new Error(this[idKey] + ' must be set'));
}
client('GET', action + (id ? id + '/' : ''))
.then(response => {
resolve(new SessionGetResponse(response.body, client));
})
.catch(error => {
reject(error);
});
});
}

list(params) {
let client = this[clientKey];
let action = this[actionKey];
return new Promise((resolve, reject) => {
client('GET', action, params)
.then(response => {
let sessions = {
api_id: response.body.apiId,
meta: response.body.meta
}
let objects = [];
Object.defineProperty(objects, 'meta', {
value: response.body.meta,
enumerable: true
});
Object.defineProperty(objects, 'apiId', {
value: response.body.apiId,
enumerable: true
})
if (response.body.sessions !== null) {
response.body.sessions.forEach(item => {
objects.push(new SessionListResponse(item, client));
});
sessions.sessions = objects
} else {
sessions.sessions = null
}
resolve(sessions);
})
.catch(error => {
reject(error);
});
});
}

validate(req) {
var id, otp
var isObject = arguments.length;
if (isObject === 1) {
id = req.id
otp = req.otp
}
let errors = validate([{
field: 'id',
value: id,
validators: ['isRequired']
}]);

if (errors) {
return errors;
}

errors = validate([{
field: 'otp',
value: otp,
validators: ['isRequired']
}]);

if (errors) {
return errors;
}

let params = {}
params.otp = otp

let client = this[clientKey];
let idField = this[idKey];
let action = this[actionKey];

return new Promise((resolve, reject) => {
client('POST', action + (id ? id + '/' : ''), params)
.then(response => {
resolve(new ValidateSessionResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
})

}
}
2 changes: 2 additions & 0 deletions lib/rest/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { ComplianceRequirementInterface } from "../resources/complianceRequireme
import { ComplianceApplicationInterface } from "../resources/complianceApplications";
import { LOAInterface } from "../resources/loa";
import { HostedMessagingNumberInterface } from "../resources/hostedMessagingNumber";
import {SessionInterface} from "../resources/verify";
import { MaskingSessionInterface } from '../resources/maskingSession.js';

export class Client {
Expand Down Expand Up @@ -124,6 +125,7 @@ export class Client {
this.multiPartyCalls = new MultiPartyCallInterface(client);
this.loa = new LOAInterface(client);
this.hostedMessagingNumber = new HostedMessagingNumberInterface(client);
this.verify_session = new SessionInterface(client);
this.maskingSession = new MaskingSessionInterface(client);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/rest/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ComplianceApplicationInterface } from "../resources/complianceApplicati
import { MultiPartyCallInterface } from "../resources/multiPartyCall";
import { LOAInterface } from "../resources/loa";
import { HostedMessagingNumberInterface } from "../resources/hostedMessagingNumber";
import { SessionInterface } from "../resources/verify";
import { MaskingSessionInterface } from "../resources/maskingSession.js";


Expand Down Expand Up @@ -112,6 +113,7 @@ export class Client {
this.multiPartyCalls = new MultiPartyCallInterface(client);
this.loa = new LOAInterface(client);
this.hostedMessagingNumber = new HostedMessagingNumberInterface(client);
this.verify_session = new SessionInterface(client);
this.maskingSession = new MaskingSessionInterface(client)
}

Expand Down
Loading

0 comments on commit 826785e

Please sign in to comment.