# songwhip-release-tasks

A service for scheduling 'tasks' that should be run once an album is released. `songwhip-api` pushes Spotify/AppleMusic/Deezer presave tasks into this service that contain user tokens. When a page is converted from 'prerelease' to 'live' the associated tasks are run adding the album to the users' libraries.

The service is deployed to [Vercel Edge Functions](https://vercel.com/docs/functions/edge-functions) which runs on a limited Javascript runtime.

## Architecture

- Uses DynamoDB to keep track of tasks
- Uses AWS SNS as a simple queue
- Uses `localstack` to emulate AWS services on local machines and Github Actions CI.

## Prerequisites

- Node.js configured repo\
  https://www.notion.so/Node-js-project-setup-guide-86feadffeb304962b06e6b1bb8f09213
- Docker (for Localstack)
- Vercel CLI (for `yarn dev:vercel`)

## Running

- Create a `.env` file based on the `.env.shadow` template
- Run `yarn dev` to spin up a local dev server and a local dockerized instance of Localstack to emulate AWS services
- Alternatively, you can configure the AWS credentials in your `.env` file to point to actual AWS services and run `yarn dev:vercel` to start a Vercel dev server

## Testing

- Run `yarn test` to check for format, lint or types errors and run the test suites
- You can also use `yarn test:e2e:watch` to run the test suites in watch mode.

> [!NOTE]
> You need some dependencies to run the `yarn test:e2e` command.
>
> ```
> brew install awscli
> brew install jq
> ```

## Localization

We are managing localization using the [@theorchard/frontend-cli-i18n](https://github.com/theorchard/orchard-suite/tree/master/packages/frontend-cli-i18n) cli tooling.

The `frontend-cli-i18n` init command scans through the repo for `*.i18n.json|md` files and constructs type safe translation files in the `./locales/` folder.

The `i18n:init` script is run after `yarn install` so that the `./locales/` folder is always generated and ready to be referenced in code.

Whenever you change a `i18n.json` or `*.i18n.md` file you need to update the locale definitions by running `yarn i18n:init`. Alternatively you can run the `yarn i18n:watch` script to watch for changes and automatically re-run init.

### Defining localized emails

Each email that should be localized should be defined in its own folder under `lib/email`.

```
lib/email/myEmail
  - index.ts            // code for constructing email
  - i18n.json           // terms for subject and other fields
  - body.i18n.md        // email body in markdown
  - textBody.i18n.md    // email text body in markdown
  - footer.i18n.md      // email footer in markdown
```

Format localized strings by using the formatter util defined in `./locale.ts`.

```ts
import { getFormatter } from '../../locale';

// create a formatter for French
const t = getFormatter('fr');

const frenchBody = t('myEmail.body', { someArg });
```

### Syncing remote translations

Talking to POEDitor requires a `POEDITOR_API_TOKEN`. Get it from your team mates or ask about it in the #frontend slack channel.

Make sure your `.env` file contains the following:

```shell
POEDITOR_PROJECT_ID=523757
POEDITOR_API_TOKEN=[your token]
```

We are syncing translations with POEditor using the `frontend-cli-i18n` tooling. The following command is run as part of the CI:

```shell
yarn i18n:sync
```

Running the i18n:sync command will upload new / updated terms and English translations to POEditor. Then download all translations in the supported locales. All locale files are stored in the `./locales/` folder.
