Skip to content

Commit

Permalink
Merge pull request #21 from guidesmiths/refactor-publishers
Browse files Browse the repository at this point in the history
Fix message when there are no celebrations
  • Loading branch information
inigomarquinez authored Apr 14, 2023
2 parents dcb8a83 + f382856 commit b77e7c6
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 185 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2

updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- Work anniversaries, including a welcome message on the first day
- Company-observed holidays

## Setup
## ⚙️ Setup

- Clone the repository and run `npm install`.
- Ensure your [AWS credentials are available](https://serverless.com/framework/docs/providers/aws/guide/credentials/).
Expand Down Expand Up @@ -40,7 +40,7 @@ To run it locally, you can create an `.env` file. To deploy it in the cloud, you

Apart from the env variables, it will read the `BAMBOOHR_KEY` secret from [AWS Secrets Manager](https://aws.amazon.com/es/secrets-manager/).

## Usage
## 🗒️ Usage

### Deployment

Expand All @@ -58,7 +58,7 @@ After successful deployment, you can invoke the deployed function by using the f
serverless invoke --function main
```

## Local development
## 🏗️ Local development

First you need to create an `.env` file with the following variables:

Expand Down Expand Up @@ -99,6 +99,11 @@ When you're finished, remember to run:
npm run infra:dev:stop
```

## 🔗 Useful links

- [Slack Block Kit documentation](https://api.slack.com/block-kit)
- [Slack Block Kit builder](https://app.slack.com/block-kit-builder)

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand All @@ -122,4 +127,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
3 changes: 1 addition & 2 deletions src/constants/defaultMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ export default {
elements: [
{
type: 'mrkdwn',
plain_text:
':mad-hatter::teapot: *A very Merry Unbirthday to you all!* :mad-hatter::teapot:',
text: ':mad-hatter::teapot: *A very Merry Unbirthday to you all!* :mad-hatter::teapot:',
},
],
};
2 changes: 1 addition & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
publishEmployeesAtOffice,
publishEmployeesCelebrations,
pusblishBankHolidays,
} from './publisher';
} from './publishers';
import { getSecret, initializeSecretsManager } from './secrets';
import { TSecrets } from '.';

Expand Down
85 changes: 85 additions & 0 deletions src/publishers/bank-holidays-publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import moment from 'moment';

import { TWhosOut } from '..';
import {
FRIDAY_ISO_WEEKDAY,
HUMAN_READABLE_DATE,
MONDAY_ISO_WEEKDAY,
} from '../common';
import { postSlackMessage } from '../http';

export const pusblishBankHolidays = async (
bankHolidays: TWhosOut,
today: moment.Moment
): Promise<void> => {
const monthBankHolidaysBlocks: any[] = [];
let nextBankHolidaysBlocks: any[] = [];

if (
bankHolidays.month.length > 0 &&
((today.date() === 1 && today.isoWeekday() <= FRIDAY_ISO_WEEKDAY) ||
(today.date() === 2 && today.isoWeekday() === MONDAY_ISO_WEEKDAY) ||
(today.date() === 3 && today.isoWeekday() === MONDAY_ISO_WEEKDAY))
) {
monthBankHolidaysBlocks.push({
type: 'context',
elements: [
{
type: 'mrkdwn',
text: '*This month bank holidays:*',
},
],
});

monthBankHolidaysBlocks.push(
...bankHolidays.month.map(holiday => ({
type: 'section',
text: {
type: 'mrkdwn',
text: `*${holiday.name}*\n${moment(holiday.start).format(
HUMAN_READABLE_DATE
)}`,
},
}))
);

monthBankHolidaysBlocks.push({
type: 'divider',
});
}

if (bankHolidays.next.length > 0) {
nextBankHolidaysBlocks = bankHolidays.next.map(holiday => ({
type: 'section',
text: {
type: 'mrkdwn',
text: `*${holiday.name}*\n${moment(holiday.start).format(
HUMAN_READABLE_DATE
)}`,
},
}));
}

if (monthBankHolidaysBlocks.length > 0 || nextBankHolidaysBlocks.length > 0) {
const message = {
text: '🏖️ Bank Holidays',
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text: '🏖️ Bank Holidays',
emoji: true,
},
},
...monthBankHolidaysBlocks,
...nextBankHolidaysBlocks,
],
};

await postSlackMessage(
process.env.BANK_HOLIDAYS_WEBHOOK_URL ?? '',
message
);
}
};
72 changes: 72 additions & 0 deletions src/publishers/employees-at-office-publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import moment from 'moment';

import {
TBambooHREmployeeAtOffice,
TBambooHREmployeeExtended,
TWhosOut,
} from '..';
import { FRIDAY_ISO_WEEKDAY } from '../common';
import { postSlackMessage } from '../http';

export const publishEmployeesAtOffice = async (
employees: TBambooHREmployeeExtended[],
employeesAtOffice: TBambooHREmployeeAtOffice[],
today: moment.Moment
): Promise<void> => {
// First filter 'employeesAtOffice' to those included in 'employees'
const filteredEmployeesAtOffice = employeesAtOffice.reduce<
TBambooHREmployeeExtended[]
>((previousValue, currentEmployee) => {
const employee = employees.find(e => e.id === currentEmployee.employeeId);
return employee ? previousValue.concat(employee) : previousValue;
}, []);

const blocks =
filteredEmployeesAtOffice.length > 0
? filteredEmployeesAtOffice.map(e => ({
type: 'context',
elements: [
{
type: 'image',
image_url: e.photoUrl,
alt_text: 'employee avatar',
},
{
type: 'mrkdwn',
text: `*${e.displayName}*`,
},
],
}))
: [
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: ':eyes: *Nobody at the office!*',
},
],
},
];

const message = {
text: `🏢 ${
today.isoWeekday() < FRIDAY_ISO_WEEKDAY ? 'Tomorrow' : 'Next Monday'
} at One Beyond offices`,
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text: `🏢 ${
today.isoWeekday() < FRIDAY_ISO_WEEKDAY ? 'Tomorrow' : 'Next Monday'
} at One Beyond offices`,
emoji: true,
},
},
...blocks,
],
};

await postSlackMessage(process.env.OFFICE_WEBHOOK_URL ?? '', message);
};
Loading

0 comments on commit b77e7c6

Please sign in to comment.