Skip to content

Commit

Permalink
fix: handle weekend closure with a message that is not confusing
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsival committed Mar 22, 2023
1 parent 62e26aa commit 5c87e43
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CHANNEL_ID=C0XXXYYYZZZ
CLOSURE_DAY=25
DEBUG=0
DEBUG_DATE=
JOB_HOUR=10
JOB_MONTH_END=11
JOB_MONTH_START=2
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The app uses `.env` config which is based on `.env.example`:

- `CHANNEL_ID` – Slack channel ID for messaging
- `ClOSURE_DAY` – day of month on which the worklog agenda closes (defaults to `25`)
- `DEBUG` – set `1` to disable sending messages and only output to console (defaults to `0`)
- `DEBUG_DATE` – when `DEBUG=1` use this to simulate a specific date for test run (defaults to `undefined`)
- `JOB_HOUR` – hour at which the messaging schedule runs daily (defaults to `10`)
- `JOB_MONTH_END` – month in which the messaging schedule ends (defaults to `11`)
- `JOB_MONTH_START` – month in which the messaging schedule starts (defaults to `2`)
Expand Down
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Range, RecurrenceRule, scheduleJob } from 'node-schedule';
import { MAX_MONTH } from './constants';
import {
CHANNEL_ID,
DEBUG,
DEBUG_DATE,
JOB_HOUR,
JOB_MONTH_END,
JOB_MONTH_START,
Expand Down Expand Up @@ -35,6 +37,12 @@ logger.logInfo(
JSON.stringify({ CHANNEL_ID, JOB_MONTH_START, JOB_MONTH_END, JOB_HOUR, TZ }),
);

if (DEBUG) {
logger.logInfo('Running in DEBUG mode, not actually sending to Slack.');
if (DEBUG_DATE) logger.logInfo(`Using DEBUG_DATE: ${DEBUG_DATE}`);
run().catch(logger.logError);
}

const shutdownGracefully = () => {
job.cancel();
process.exit(0);
Expand Down
10 changes: 8 additions & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
SUNDAY,
SUNDAY_DIFF,
} from './constants';
import { CLOSURE_DAY } from './environment';
import { CLOSURE_DAY, DEBUG_DATE } from './environment';

export const now = () => new Date();
export const now = () => (DEBUG_DATE ? new Date(DEBUG_DATE) : new Date());

export const formatMonth = (date: Date) => date.getMonth() + 1;

Expand All @@ -30,3 +30,9 @@ export const getFirstNotificationDay = () =>

export const getLastNotificationDay = () =>
getFinalDay() - LAST_NOTIFICATION_DAYS;

export const isFinalDayAtWeekend = () => {
const date = now();
date.setDate(CLOSURE_DAY);
return [SATURDAY, SUNDAY].includes(date.getDay());
};
2 changes: 2 additions & 0 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ dotenv.config();

export const CHANNEL_ID = process.env.CHANNEL_ID ?? '';
export const CLOSURE_DAY = Number(process.env.CLOSURE_DAY ?? '25');
export const DEBUG = process.env.DEBUG === '1';
export const DEBUG_DATE = DEBUG ? process.env.DEBUG_DATE : undefined;
export const JOB_HOUR = Number(process.env.JOB_HOUR ?? '10');
export const JOB_MONTH_END = Number(process.env.JOB_MONTH_END ?? '11');
export const JOB_MONTH_START = Number(process.env.JOB_MONTH_START ?? '2');
Expand Down
18 changes: 15 additions & 3 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
JANUARY,
LAST_NOTIFICATION_DAYS,
} from './constants';
import { formatMonth, getFinalDay, now } from './date';
import { formatMonth, getFinalDay, isFinalDayAtWeekend, now } from './date';
import { CLOSURE_DAY } from './environment';
import { NotificationDay } from './types';

Expand All @@ -27,8 +27,20 @@ const getPeriod = () => {
return `${startDay}.${startMonth}. – ${endDay}.${endMonth}. včetně`;
};

export const createFirstMessage = () =>
`Ahoj <!channel> 👋, blíží se nám další uzávěrka fakturačního období, tentokrát *${getPeriod()}*. Začněte si prosím chystat vaše worklogy a mějte vše *${getFinalDay()}. do 12:00* připraveno. Díky moc! 🫶`;
export const createFirstMessage = () => {
const message = [
`Ahoj <!channel> 👋, blíží se nám další uzávěrka fakturačního období, tentokrát *${getPeriod()}*.`,
];
if (isFinalDayAtWeekend()) {
message.push(
`Jelikož nám konec období vychází na víkend, musíme vše uzavřít v pátek.`,
);
}
message.push(
`Začněte si prosím chystat vaše worklogy a mějte vše *${getFinalDay()}. do 12:00* připraveno. Díky moc! 🫶`,
);
return message.join(' ');
};

export const createNotificationMessage = (day: NotificationDay) => {
const days = day === LAST_NOTIFICATION_DAYS ? 'den' : 'dny';
Expand Down
15 changes: 9 additions & 6 deletions src/send-message.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { client } from './client';
import { CHANNEL_ID } from './environment';
import { CHANNEL_ID, DEBUG } from './environment';
import { logger } from './logger';
import { SendMessageProperties } from './types';

const handleSend = async ({ text }: SendMessageProperties) =>
client.chat.postMessage({
channel: CHANNEL_ID,
mrkdwn: true,
text,
});

const formatError = (error: unknown) => {
if (error instanceof Error) return error;
return new Error(String(error));
};

export const sendMessage = async ({ text }: SendMessageProperties) => {
try {
await client.chat.postMessage({
channel: CHANNEL_ID,
mrkdwn: true,
text,
});
if (!DEBUG) await handleSend({ text });
logger.logSuccess(`SENT: ${text}`);
} catch (error: unknown) {
logger.logError(formatError(error));
Expand Down

0 comments on commit 5c87e43

Please sign in to comment.