Explorar el Código

push 26

master
jefry hace 4 años
padre
commit
6329553719
Se han modificado 3 ficheros con 49 adiciones y 18 borrados
  1. +6
    -5
      api/message/models/message.settings.json
  2. +10
    -1
      api/message/services/message.js
  3. +33
    -12
      config/functions/cron.js

+ 6
- 5
api/message/models/message.settings.json Ver fichero

@@ -11,11 +11,6 @@
"draftAndPublish": true "draftAndPublish": true
}, },
"attributes": { "attributes": {
"send_from": {
"type": "string",
"regex": "\\+?([ -]?\\d+)+|\\(\\d+\\)([ -]\\d+)",
"required": true
},
"send_to": { "send_to": {
"type": "string", "type": "string",
"regex": "\\+?([ -]?\\d+)+|\\(\\d+\\)([ -]\\d+)", "regex": "\\+?([ -]?\\d+)+|\\(\\d+\\)([ -]\\d+)",
@@ -42,6 +37,12 @@
"state": { "state": {
"type": "string", "type": "string",
"private": false "private": false
},
"raw_text": {
"type": "string"
},
"session_datum_key": {
"model": "session-data"
} }
} }
} }

+ 10
- 1
api/message/services/message.js Ver fichero

@@ -5,4 +5,13 @@
* to customize this service * to customize this service
*/ */


module.exports = {};
module.exports = {
distinctbyKey: async (params) =>{
const results = await strapi.query('message').find(params);
let arrKeys = [];
results.forEach((i)=>{
if(!arrKeys.includes(i["session_datum_key"])) arrKeys.push(i["session_datum_key"]);
});
return arrKeys
}
};

+ 33
- 12
config/functions/cron.js Ver fichero

@@ -1,5 +1,6 @@
"use strict"; "use strict";

const messageModel = require("../../model/messageModel");
const { parseMultipartData, sanitizeEntity } = require("strapi-utils");
/** /**
* Cron config that gives you an opportunity * Cron config that gives you an opportunity
* to run scheduled jobs. * to run scheduled jobs.
@@ -9,21 +10,41 @@
* *
* See more details here: https://strapi.io/documentation/developer-docs/latest/concepts/configurations.html#cron-tasks * See more details here: https://strapi.io/documentation/developer-docs/latest/concepts/configurations.html#cron-tasks
*/ */
let clients = {};
module.exports = { module.exports = {
"*/3 * * * * *": async () => {
const draftArticleToPublish = await strapi.api.message.services.message.find(
"*/10 * * * * *": async () => {
const session_key = await strapi.api.message.services.message.distinctbyKey(
{ {
_sort: "send_from:desc",
_sort: "session_datum_key:desc",
state: "QUEUE", state: "QUEUE",
} }
); );

draftArticleToPublish.forEach(async (message) => {
await strapi.api.message.services.message.update(
{ id: message.id },
{ state: "SENT" }
);
});
session_key.forEach(async (i)=>{
if(!Object.keys(clients).includes(`${i["id"]}`)){
clients[i["id"]] = null;
messageModel.loadClient(i["session_data"],async (data)=>{
var json = JSON.parse(data);
if (json["STATUS"] == 1 && json["TYPE"] == "READY") {
return clients[i["id"]] = json["DATA"];
}
else if(json["STATUS"] ==0){
return;
}
});
}
})
console.log(clients);
// const messages = await strapi.api.message.services.message.find(
// {
// _sort: "session_datum_key:desc",
// state: "QUEUE",
// }
// );
// messages.forEach(async (message) => {
// await strapi.api.message.services.message.update(
// { id: message.id },
// { state: "SENT" }
// );
// });
}, },
}; };

Cargando…
Cancelar
Guardar