Skip to content

Commit

Permalink
Fixed the methods working
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyShark committed Jun 21, 2024
1 parent 130b3e4 commit e7ec9ee
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
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

0 comments on commit e7ec9ee

Please sign in to comment.