diff --git a/src/utils/CommonUtils.js b/src/utils/CommonUtils.js index 99660e1..a8b993b 100644 --- a/src/utils/CommonUtils.js +++ b/src/utils/CommonUtils.js @@ -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!`) @@ -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) @@ -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) { diff --git a/src/utils/MessageAPI.js b/src/utils/MessageAPI.js index aebbafb..abbfc4a 100644 --- a/src/utils/MessageAPI.js +++ b/src/utils/MessageAPI.js @@ -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 } @@ -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)) } /** @@ -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) {