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

Fixed the methods working #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/utils/CommonUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class CommonUtils {
throw new Error(`${name} must be an integer!`)
}

static validateBoolean(name, val){
if(Object.prototype.toString.call(val) !== '[object Boolean]')
throw new Error(`${name} must be an Boolean!`)
}

static validateNumber(name, val) {
if (!val || !Number(val))
throw new Error(`${name} must be a number!`)
Expand All @@ -31,6 +36,10 @@ class CommonUtils {
}
}

static generateMethodURLWithQuery(params, method, queryParams) {
return `${params.host}/waInstance${params.idInstance}/${method}/${params.apiTokenInstance}${queryParams}`
}

static validateChatIdPhoneNumber(chatId, phoneNumber) {
if (!chatId) {
CommonUtils.validateInteger('phoneNumber', phoneNumber)
Expand All @@ -41,8 +50,9 @@ class CommonUtils {
}

static validateArray(name, val) {
if (!val || !Array.isArray(val))
throw new Error(`${name} must be an Array!`)
if (!val || !Array.isArray(val)){
throw new Error(`${name} must be an Array! `)
}
}

static validatePath(name, val) {
Expand Down
78 changes: 70 additions & 8 deletions src/utils/MessageAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class MessageAPI {
}

if (quotedMessageId !== null) {
CommonUtils.validateString('quotedMessageId', quotedMessageId)
postData['quotedMessageId'] = quotedMessageId
}
if (linkPreview !== null) {
CommonUtils.validateBoolean('linkPreview', linkPreview)
postData['linkPreview'] = linkPreview
}

Expand Down Expand Up @@ -291,21 +293,38 @@ class MessageAPI {
}

/**
* Returns array of Message objects
*/
async lastIncomingMessages() {
* @param {Number} minutes Optional
*/

async lastIncomingMessages(minutes = null) {
const method = 'lastIncomingMessages';
const response = await axios.get(CommonUtils.generateMethodURL(this._restAPI.params, method));
return response.data.map((msg) => new Message(msg))
let response;

if (minutes !== null) {
CommonUtils.validateInteger('minutes', minutes);
response = await axios.get(CommonUtils.generateMethodURLWithQuery(this._restAPI.params, method, `?minutes=${minutes}`));
} else {
response = await axios.get(CommonUtils.generateMethodURL(this._restAPI.params, method));
}

return response.data.map((msg) => new IncomingMessage(msg));
}

/**
* Returns array of Message objects
*/
async lastOutgoingMessages() {
async lastOutgoingMessages(minutes = null) {
const method = 'lastOutgoingMessages';
const response = await axios.get(CommonUtils.generateMethodURL(this._restAPI.params, method));
return response.data.map((msg) => new Message(msg))
let response;

if (minutes !== null) {
CommonUtils.validateInteger('minutes', minutes);
response = await axios.get(CommonUtils.generateMethodURLWithQuery(this._restAPI.params, method, `?minutes=${minutes}`));
} else {
response = await axios.get(CommonUtils.generateMethodURL(this._restAPI.params, method));
}

return response.data.map((msg) => new OutgoingMessage(msg))
}

/**
Expand Down Expand Up @@ -405,6 +424,49 @@ class Message {
this.typeMessage = data.typeMessage;
}
}
class OutgoingMessage{
constructor(data){
this.type = data.type
this.idMessage = data.idMessage
this.timestamp = data.timestamp
this.typeMessage = data.typeMessage
this.chatId = data.chatId
if(data["textMessage"] !== undefined){
this.textMessage = data.textMessage
}
if(data["extendedTextMessage"] !== undefined){
this.extendedTextMessage = data.extendedTextMessage
}
if(data["quotedMessage"]!== undefined) {
this.quotedMessage = data.quotedMessage
}
this.statusMessage = data.statusMessage
this.sendByApi = data.sendByApi

}
}

class IncomingMessage {
constructor(data){
this.type = data.type
this.idMessage = data.idMessage
this.timestamp = data.timestamp
this.typeMessage = data.typeMessage
this.chatId = data.chatId
if(data["textMessage"] !== undefined){
this.textMessage = data.textMessage
}
if(data["extendedTextMessage"] !== undefined){
this.extendedTextMessage = data.extendedTextMessage
}
if(data["quotedMessage"]!== undefined) {
this.quotedMessage = data.quotedMessage
}
this.senderId = data.senderId
this.senderName = data.senderName
this.senderContactName = data.senderContactName
}
}

class QueueMessage {
constructor(data) {
Expand Down
Loading