|
| 1 | +const db = require('../db/connection'); |
| 2 | +const { getSandboxNameFromMessage } = require('../common'); |
| 3 | + |
| 4 | +function getSandboxDetails(channel, sandboxName) { |
| 5 | + return new Promise((resolve, reject) => { |
| 6 | + db.get( |
| 7 | + 'SELECT owner, assigned_at FROM sandboxes WHERE team = $teamChannel AND sandbox = $sandboxName', |
| 8 | + { $teamChannel: channel, $sandboxName: sandboxName }, |
| 9 | + (err, row) => { |
| 10 | + if (err || !row) { |
| 11 | + return reject(err || 'sandbox does not exist'); |
| 12 | + } |
| 13 | + return resolve(row); |
| 14 | + }, |
| 15 | + ); |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function updateAssignedAt(channel, sandboxName, newAssignedAt) { |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + db.run( |
| 22 | + 'UPDATE sandboxes SET assigned_at = $newAssignedAt WHERE team = $teamChannel AND sandbox = $sandboxName', |
| 23 | + { |
| 24 | + $newAssignedAt: newAssignedAt.toUTCString(), |
| 25 | + $teamChannel: channel, |
| 26 | + $sandboxName: sandboxName, |
| 27 | + }, |
| 28 | + (err) => { |
| 29 | + if (err) return reject(err); |
| 30 | + return resolve(); |
| 31 | + }, |
| 32 | + ); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function addWorkingDays(startDate, workingDays) { |
| 37 | + const date = new Date(startDate.getTime()); |
| 38 | + let remaining = workingDays; |
| 39 | + while (remaining > 0) { |
| 40 | + date.setDate(date.getDate() + 1); |
| 41 | + const day = date.getDay(); |
| 42 | + if (day !== 0 && day !== 6) { |
| 43 | + remaining -= 1; |
| 44 | + } |
| 45 | + } |
| 46 | + return date; |
| 47 | +} |
| 48 | + |
| 49 | +function formatDate(date) { |
| 50 | + return date.toISOString().slice(0, 10); |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = { |
| 54 | + pattern: /(przedluzam|extending) (sandbox|adeng|neutron-api)-|^[pe] (sandbox|adeng|neutron-api)-/i, |
| 55 | + action({ message, say }) { |
| 56 | + const sandboxName = getSandboxNameFromMessage(message); |
| 57 | + let msg = `<@${message.user}> `; |
| 58 | + |
| 59 | + if (!sandboxName) { |
| 60 | + say(`${msg}:x: - can't find sandbox name in your message`); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + getSandboxDetails(message.channel, sandboxName) |
| 65 | + .then(async ({ owner }) => { |
| 66 | + if (!owner) { |
| 67 | + msg += ':-1: - sandbox is not currently booked'; |
| 68 | + say(msg); |
| 69 | + return; |
| 70 | + } |
| 71 | + if (owner !== message.user) { |
| 72 | + msg += `:-1: - <@${owner}> is using it`; |
| 73 | + say(msg); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Reset assigned_at to now to grant a fresh 5 working day window |
| 78 | + const now = new Date(); |
| 79 | + await updateAssignedAt(message.channel, sandboxName, now); |
| 80 | + const due = addWorkingDays(now, 5); |
| 81 | + |
| 82 | + msg += `:+1: (extended, new due date ${formatDate(due)})`; |
| 83 | + say(msg); |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + say(`:x: sandbot error: \`${err}\`, try again`); |
| 87 | + }); |
| 88 | + }, |
| 89 | +}; |
0 commit comments