|
| 1 | +// imports from node modules |
| 2 | +const { parentPort } = require("worker_threads"); |
| 3 | +const { Op } = require("sequelize"); |
| 4 | +const { v4: uuidv4 } = require("uuid"); |
| 5 | + |
| 6 | +//Local Imports |
| 7 | +const models = require("../../models"); |
| 8 | +const { trimURL, getSupportContactEmails } = require("../../utils/authUtil"); |
| 9 | +const { trim } = require("lodash"); |
| 10 | + |
| 11 | +// Constants |
| 12 | +const User = models.user; |
| 13 | +const UserRoles = models.UserRoles; |
| 14 | +const RoleTypes = models.RoleTypes; |
| 15 | +const NotificationQueue = models.notification_queue; |
| 16 | +const accountUnlockLink = `${trimURL(process.env.WEB_URL)}`; |
| 17 | + |
| 18 | +const accountDeleteAlertDaysForUser = |
| 19 | + require("../../config/monitorings.js").accountDeleteAlertDaysForUser; |
| 20 | + |
| 21 | +const updateUserAndSendNotification = async (user, daysToExpiry, version) => { |
| 22 | + const expiryDate = new Date( |
| 23 | + Date.now() + 1000 * 60 * 60 * 24 * daysToExpiry |
| 24 | + ).toDateString(); |
| 25 | + |
| 26 | + // Queue notification |
| 27 | + await NotificationQueue.create({ |
| 28 | + type: "email", |
| 29 | + templateName: "accountDeleteWarning", |
| 30 | + notificationOrigin: "Account Delete Warning", |
| 31 | + deliveryType: "immediate", |
| 32 | + createdBy: "System", |
| 33 | + updatedBy: "System", |
| 34 | + metaData: { |
| 35 | + notificationId: uuidv4(), |
| 36 | + recipientName: `${user.dataValues.firstName}`, |
| 37 | + notificationOrigin: "Account Delete Warning", |
| 38 | + subject: "Account Deletion Warning", |
| 39 | + mainRecipients: [user.dataValues.email], |
| 40 | + notificationDescription: "Account Delete Warning", |
| 41 | + daysToExpiry: daysToExpiry, |
| 42 | + expiryDate: expiryDate, |
| 43 | + accountUnlockLink: accountUnlockLink, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + await user.update({ |
| 48 | + metaData: { |
| 49 | + ...user.metaData, |
| 50 | + accountDeleteEmailSent: { |
| 51 | + ...user.metaData.accountDeleteEmailSent, |
| 52 | + [version]: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +(async () => { |
| 59 | + try { |
| 60 | + parentPort && |
| 61 | + parentPort.postMessage({ |
| 62 | + level: "info", |
| 63 | + text: "Account Deletion Job started ...", |
| 64 | + }); |
| 65 | + |
| 66 | + //get all relevant dates in easy to understand variables |
| 67 | + const now = Date.now(); |
| 68 | + const deletionDate = now - 1000 * 60 * 60 * 24 * 180; // 180 days |
| 69 | + const firstDeleteWarningDate = |
| 70 | + deletionDate + 1000 * 60 * 60 * 24 * accountDeleteAlertDaysForUser[0]; // first accountDeleteAlertDaysForUser[0] days |
| 71 | + |
| 72 | + //get all users where lastLoginAt is older than firstDeleteWarningDate |
| 73 | + const users = await User.findAll({ |
| 74 | + where: { |
| 75 | + lastLoginAt: { |
| 76 | + [Op.lt]: firstDeleteWarningDate, |
| 77 | + }, |
| 78 | + }, |
| 79 | + include: [ |
| 80 | + { |
| 81 | + model: UserRoles, |
| 82 | + attributes: ["id"], |
| 83 | + as: "roles", |
| 84 | + include: [ |
| 85 | + { |
| 86 | + model: RoleTypes, |
| 87 | + as: "role_details", |
| 88 | + attributes: ["id", "roleName"], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + |
| 95 | + //filter out admins and owners |
| 96 | + const filteredUsers = users.filter((user) => { |
| 97 | + return !user.roles.some((role) => { |
| 98 | + return ( |
| 99 | + role?.role_details?.roleName === "administrator" || |
| 100 | + role?.role_details?.roleName === "owner" |
| 101 | + ); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + for (const user of filteredUsers) { |
| 106 | + //pull out the internal user object for easier use below |
| 107 | + let userInternal = user.dataValues; |
| 108 | + const lastLoginAt = userInternal.lastLoginAt; |
| 109 | + const daysToExpiry = |
| 110 | + Math.floor((lastLoginAt - deletionDate) / (1000 * 60 * 60 * 24)) + 1; |
| 111 | + |
| 112 | + if ( |
| 113 | + daysToExpiry <= accountDeleteAlertDaysForUser[0] && |
| 114 | + daysToExpiry > accountDeleteAlertDaysForUser[1] && |
| 115 | + !userInternal.metaData?.accountDeleteEmailSent?.first |
| 116 | + ) { |
| 117 | + parentPort && |
| 118 | + parentPort.postMessage({ |
| 119 | + level: "verbose", |
| 120 | + text: |
| 121 | + "User with email " + |
| 122 | + userInternal.email + |
| 123 | + " is within " + |
| 124 | + daysToExpiry + |
| 125 | + " days of account deletion due to inactivity.", |
| 126 | + }); |
| 127 | + |
| 128 | + let version = "first"; |
| 129 | + await updateUserAndSendNotification(user, daysToExpiry, version); |
| 130 | + } |
| 131 | + |
| 132 | + if ( |
| 133 | + daysToExpiry <= accountDeleteAlertDaysForUser[1] && |
| 134 | + daysToExpiry > accountDeleteAlertDaysForUser[2] && |
| 135 | + !userInternal.metaData?.accountDeleteEmailSent?.second |
| 136 | + ) { |
| 137 | + parentPort && |
| 138 | + parentPort.postMessage({ |
| 139 | + level: "verbose", |
| 140 | + text: |
| 141 | + "User with email " + |
| 142 | + userInternal.email + |
| 143 | + " is within " + |
| 144 | + daysToExpiry + |
| 145 | + " days of account deletion due to inactivity.", |
| 146 | + }); |
| 147 | + let version = "second"; |
| 148 | + await updateUserAndSendNotification(user, daysToExpiry, version); |
| 149 | + } |
| 150 | + if ( |
| 151 | + daysToExpiry <= accountDeleteAlertDaysForUser[2] && |
| 152 | + daysToExpiry > 0 && |
| 153 | + !userInternal.metaData?.accountDeleteEmailSent?.third |
| 154 | + ) { |
| 155 | + parentPort && |
| 156 | + parentPort.postMessage({ |
| 157 | + level: "verbose", |
| 158 | + text: |
| 159 | + "User with email " + |
| 160 | + userInternal.email + |
| 161 | + " is within " + |
| 162 | + daysToExpiry + |
| 163 | + " days of account deletion due to inactivity", |
| 164 | + }); |
| 165 | + |
| 166 | + let version = "third"; |
| 167 | + await updateUserAndSendNotification(user, daysToExpiry, version); |
| 168 | + } |
| 169 | + |
| 170 | + if ( |
| 171 | + daysToExpiry <= 0 && |
| 172 | + !userInternal.metaData?.accountDeleteEmailSent?.final |
| 173 | + ) { |
| 174 | + parentPort && |
| 175 | + parentPort.postMessage({ |
| 176 | + level: "verbose", |
| 177 | + text: |
| 178 | + "User with email " + |
| 179 | + userInternal.email + |
| 180 | + " has been removed due to inactivity.", |
| 181 | + }); |
| 182 | + |
| 183 | + const accountUnlockLink = `${trimURL(process.env.WEB_URL)}/register`; |
| 184 | + // Queue notification |
| 185 | + await NotificationQueue.create({ |
| 186 | + type: "email", |
| 187 | + templateName: "accountDeleted", |
| 188 | + notificationOrigin: "Account Deleted", |
| 189 | + deliveryType: "immediate", |
| 190 | + createdBy: "System", |
| 191 | + updatedBy: "System", |
| 192 | + metaData: { |
| 193 | + notificationId: uuidv4(), |
| 194 | + recipientName: `${userInternal.firstName}`, |
| 195 | + notificationOrigin: "Account Deleted", |
| 196 | + subject: "Account Deleted", |
| 197 | + mainRecipients: [userInternal.email], |
| 198 | + notificationDescription: "Account Deleted", |
| 199 | + accountUnlockLink: accountUnlockLink, |
| 200 | + }, |
| 201 | + }); |
| 202 | + |
| 203 | + // delete user account |
| 204 | + // TODO -- Move user to archive table |
| 205 | + await user.destroy(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + parentPort && |
| 210 | + parentPort.postMessage({ |
| 211 | + level: "info", |
| 212 | + text: "Account lock check job completed ...", |
| 213 | + }); |
| 214 | + } catch (error) { |
| 215 | + parentPort && |
| 216 | + parentPort.postMessage({ level: "error", text: error.message }); |
| 217 | + } |
| 218 | +})(); |
0 commit comments