Untitled

 avatar
unknown
plain_text
6 months ago
3.1 kB
4
Indexable
  const survivalsQueue = new Queue('survivalsQueue', redisConfig);
  const survivalsCheckQueue = new Queue('survivalsCheckQueue', redisConfig);

  survivalsQueue.process(async (job) => {
    const { 
      internalMsgId,
      updateSendStatus = false,
      templateKey,
      templateRaw,
      options,
      to
    } = job.data;

    if (!to || !internalMsgId ||  (!templateRaw && !templateKey)) return Promise.resolve(); 
    
    const { userId, token, data, lang } = to

    const isSurvivalActive = await UserService.getSurvivalStatus({ userId })

    // Controllo se è ancora attiva
    if (!isSurvivalActive) return

    const template = { subject: null, text: null }

    // Init template
    if (templateRaw) {
      template.subject = templateRaw.subject
      template.text = templateRaw.body
    } else {
      const templateFromDb = await TemplateMessagesService.getByKey(templateKey)

      if (!templateFromDb.subject || !templateFromDb.body) return Promise.resolve(); 

      template.subject = templateFromDb.subject
      template.text = templateFromDb.body
    }
    // --

    // Send
    if (!lang || !token) return

    const message = {
      token: token,
      data: renderTemplate(lang.toLowerCase(), data)
    }

    try {
      const response = await messaging().send(message)
      console.log(response.successCount + ' messages were sent successfully')

      // Check message confirm
      survivalsCheckQueue.add({
        to,
        updateSendStatus,
        templateKey,
        templateRaw,
        internalMsgId,
        updateSendStatus
      }, {  delay: 5 * 60 * 1000 });
      /* updateSendStatus && messagesService.updateStatusByUserIds({ id: internalMsgId, status: true }) */
    } catch (error) {
      console.error(error)
      /* updateSendStatus && messagesService.updateStatusByUserIds({ id: internalMsgId, status: false, errorMsg: error }) */
    }
    

    return Promise.resolve(); 
  });

  // Block Survival Check
  survivalsCheckQueue.process(async (job) => {
    const { 
      internalMsgId,
      updateSendStatus = false,
      templateKey,
      templateRaw,
      options,
      to
    } = job.data;

    if (!userId || !internalMsgId) return Promise.resolve(); 

    const { userId } = to

    const isSurvivalActive = await SurvivalService.getStatus({ userId })

    // Controllo se è ancora attiva
    if (!isSurvivalActive) return

    // check if user sent confirm
    const message = await messagesService.getByUserId({ userId })

    // Check if message is confirmed. replyConfirm?! is the field? 
    if (message.replyConfirm === false) {
      await Promise.all([
        UserService.disableSurvivalModeById({ userId }),
        AlarmService.create({ userId })
      ])
    } else {
      survivalsQueue.add({
        internalMsgId,
        updateSendStatus,
        templateKey,
        templateRaw,
        options,
        to
      }, {  delay: 60 * 60 * 1000 })
    }
  })
Editor is loading...
Leave a Comment