Skip to content

Commit

Permalink
Merge 2394ca5 into 7b35a47
Browse files Browse the repository at this point in the history
  • Loading branch information
victorshp committed Aug 31, 2021
2 parents 7b35a47 + 2394ca5 commit f64432c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 107 deletions.
4 changes: 2 additions & 2 deletions examples/template-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// const { Client } = require('@zenvia/sdk');
const { Client } = require('../dist');

const client = new Client(process.env.ZENVIA_API_TOKEN);
const client = new Client('GKkj0dSHDN60yLdkZzVyj4-osKlMQXQj-5d4');

client.getTemplate('template-identifier')
client.getTemplate('eb5f5a45-c4f2-4086-8f41-6cc423d4fac8')
.then(response => {
console.log('Response:', response);
})
Expand Down
2 changes: 1 addition & 1 deletion examples/template-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const { Client } = require('../dist');

const client = new Client(process.env.ZENVIA_API_TOKEN);
const client = new Client('GKkj0dSHDN60yLdkZzVyj4-osKlMQXQj-5d4');

client.listTemplates()
.then(response => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenvia/sdk",
"version": "2.1.0",
"version": "2.1.1",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
18 changes: 2 additions & 16 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,7 @@ export class Client {
*/
async listTemplates(): Promise<ITemplate[]> {
const path = '/v2/templates';
return request.get(this.token, path, this.logger)
.then((templates) => {
templates.forEach((template) => {
template.channels.forEach((channel) => {
(channel.type as any) = channel.type.toLowerCase();
});
});
return templates;
});
return request.get(this.token, path, this.logger);
}

/**
Expand All @@ -175,13 +167,7 @@ export class Client {
*/
async getTemplate(id: string): Promise<ITemplate> {
const path = `/v2/templates/${id}`;
return request.get(this.token, path, this.logger)
.then((template: ITemplate) => {
template.channels.forEach((channel) => {
(channel.type as any) = channel.type.toLowerCase();
});
return template;
});
return request.get(this.token, path, this.logger);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/types/zenvia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,17 @@ export interface ITemplate {
name: string;
locale: string;
channel: string;
senderId: string;
category: string;
textReference?: string;
components: IComponents;
senderId: string;
status?: TemplateStatus;
examples?: {
[fieldName: string]: string;
};
notificationEmail?: string;
text?: string;
fields?: string[];
status?: TemplateStatus;
comments?: IComment[];
suggestions?: ISuggestions[];
channels?: IChannels[];
createdAt?: string;
updatedAt?: string;
}
Expand Down
92 changes: 10 additions & 82 deletions test/lib/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ describe('Client', () => {
};

const expected = /^----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="batch"\r\nContent-Type: application\/json\r\n\r\n{"name":"SOME_BATCH","channel":"sms","message":{"from":"FROM","contents":\[{"type":"text","text":"some text message"}\]},"columnMapper":{"recipient_header_name":"recipient_number_column","name":"recipient_name_column","protocol":"protocol_column"}}\r\n----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="contacts"; filename="file\.csv"\r\nContent-Type: text\/csv\r\n\r\ntelefone\n5511999999999\r\n----------------------------[0-9]{24}--\r\n$/m;

const zenviaNock = nock('https://api.zenvia.com')
.post('/v2/message-batches', expected)
.matchHeader('X-API-Token', 'SOME_TOKEN')
Expand Down Expand Up @@ -781,7 +781,7 @@ describe('Client', () => {
};

const expected = /^----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="batch"\r\nContent-Type: application\/json\r\n\r\n{"name":"SOME_BATCH","channel":"sms","message":{"from":"FROM","contents":\[{"type":"text","text":"some text message"}\]},"columnMapper":{"recipient_header_name":"recipient_number_column","name":"recipient_name_column","protocol":"protocol_column"}}\r\n----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="contacts"; filename="contacts\.csv"\r\nContent-Type: text\/csv\r\n\r\ntelefone\n5511999999999\r\n----------------------------[0-9]{24}--\r\n$/m;

const zenviaNock = nock('https://api.zenvia.com')
.post('/v2/message-batches', expected)
.matchHeader('X-API-Token', 'SOME_TOKEN')
Expand Down Expand Up @@ -945,7 +945,7 @@ describe('Client', () => {
};

const expected = /^----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="batch"\r\nContent-Type: application\/json\r\n\r\n{"name":"SOME_BATCH","channel":"whatsapp","message":{"from":"FROM","contents":\[{"type":"template","templateId":"a whatsapp template id"}\]},"columnMapper":{"recipient_header_name":"recipient_number_column","name":"recipient_name_column","protocol":"protocol_column"}}\r\n----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="contacts"; filename="file\.csv"\r\nContent-Type: text\/csv\r\n\r\ntelefone\n5511999999999\r\n----------------------------[0-9]{24}--\r\n$/m;

const zenviaNock = nock('https://api.zenvia.com')
.post('/v2/message-batches', expected)
.matchHeader('X-API-Token', 'SOME_TOKEN')
Expand Down Expand Up @@ -999,7 +999,7 @@ describe('Client', () => {
};

const expected = /^----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="batch"\r\nContent-Type: application\/json\r\n\r\n{"name":"SOME_BATCH","channel":"whatsapp","message":{"from":"FROM","contents":\[{"type":"template","templateId":"a whatsapp template id"}\]},"columnMapper":{"recipient_header_name":"recipient_number_column","name":"recipient_name_column","protocol":"protocol_column"}}\r\n----------------------------[0-9]{24}\r\nContent-Disposition: form-data; name="contacts"; filename="contacts\.csv"\r\nContent-Type: text\/csv\r\n\r\ntelefone\n5511999999999\r\n----------------------------[0-9]{24}--\r\n$/m;

const zenviaNock = nock('https://api.zenvia.com')
.post('/v2/message-batches', expected)
.matchHeader('X-API-Token', 'SOME_TOKEN')
Expand Down Expand Up @@ -1470,37 +1470,8 @@ describe('Client', () => {
describe('Templates', () => {

it('should list templates', async () => {
const requestTemplates = [{
name: 'Sandbox - Shipping Update',
locale: 'pt_BR',
channel: 'WHATSAPP',
category: 'SHIPPING_UPDATE',
text: '{{name}}, informamos que o seu produto {{productName}} foi enviado para a transportadora e tem previsão de chegada em {{deliveryDate}}.',
components: {
body: {
type: 'TEXT_TEMPLATE',
text: '{{name}}, informamos que o seu produto {{productName}} foi enviado para a transportadora e tem previsão de chegada em {{deliveryDate}}.'
},
},
senderId: 'detailed-gasosaurus',
fields: [
'name',
'productName',
'deliveryDate',
],
status: 'APPROVED',
comments: [],
channels: [
{
type: 'WHATSAPP',
senderId: 'detailed-gasosaurus',
status: 'APPROVED',
},
],
createdAt: '2020-06-22T20:35:05.491Z',
updatedAt: '2020-06-22T20:35:05.491Z',
}];
const responseTemplates = [{
const responseTemplates: ITemplate[] = [{
id: 'SOME_ID',
name: 'Sandbox - Shipping Update',
locale: 'pt_BR',
channel: 'WHATSAPP',
Expand All @@ -1520,20 +1491,13 @@ describe('Client', () => {
],
status: 'APPROVED',
comments: [],
channels: [
{
type: 'whatsapp',
senderId: 'detailed-gasosaurus',
status: 'APPROVED',
},
],
createdAt: '2020-06-22T20:35:05.491Z',
updatedAt: '2020-06-22T20:35:05.491Z',
}];
const zenviaNock = nock('https://api.zenvia.com')
.get('/v2/templates')
.matchHeader('X-API-Token', 'SOME_TOKEN')
.reply(200, requestTemplates);
.reply(200, responseTemplates);

const client = new Client('SOME_TOKEN');
const actualMessageResponse = await client.listTemplates();
Expand All @@ -1542,37 +1506,8 @@ describe('Client', () => {
});

it('should get template with id', async () => {
const requestTemplates = {
name: 'Sandbox - Shipping Update',
locale: 'pt_BR',
channel: 'WHATSAPP',
category: 'SHIPPING_UPDATE',
text: '{{name}}, informamos que o seu produto {{productName}} foi enviado para a transportadora e tem previsão de chegada em {{deliveryDate}}.',
components: {
body: {
type: 'TEXT_TEMPLATE',
text: '{{name}}, informamos que o seu produto {{productName}} foi enviado para a transportadora e tem previsão de chegada em {{deliveryDate}}.'
},
},
senderId: 'detailed-gasosaurus',
fields: [
'name',
'productName',
'deliveryDate',
],
status: 'APPROVED',
comments: [],
channels: [
{
type: 'WHATSAPP',
senderId: 'detailed-gasosaurus',
status: 'APPROVED',
},
],
createdAt: '2020-06-22T20:35:05.491Z',
updatedAt: '2020-06-22T20:35:05.491Z',
};
const responseTemplates = {
const responseTemplates: ITemplate = {
id: 'SOME_ID',
name: 'Sandbox - Shipping Update',
locale: 'pt_BR',
channel: 'WHATSAPP',
Expand All @@ -1592,20 +1527,13 @@ describe('Client', () => {
],
status: 'APPROVED',
comments: [],
channels: [
{
type: 'whatsapp',
senderId: 'detailed-gasosaurus',
status: 'APPROVED',
},
],
createdAt: '2020-06-22T20:35:05.491Z',
updatedAt: '2020-06-22T20:35:05.491Z',
};
const zenviaNock = nock('https://api.zenvia.com')
.get('/v2/templates/SOME_TEMPLATE_ID')
.matchHeader('X-API-Token', 'SOME_TOKEN')
.reply(200, requestTemplates);
.reply(200, responseTemplates);

const client = new Client('SOME_TOKEN');
const actualMessageResponse = await client.getTemplate('SOME_TEMPLATE_ID');
Expand Down

0 comments on commit f64432c

Please sign in to comment.