Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dd31c65
feat(volunteers): add findActiveByFosterTypes query
Yurika-Kan Oct 27, 2026
5ef68ce
chore(deps): add @aws-sdk/client-scheduler
Yurika-Kan Oct 27, 2026
5b9243c
feat(notifications): add bulk email endpoint with scheduled sends
Yurika-Kan Oct 27, 2026
7a6e642
feat(infra): add notification dispatch lambda and aws setup docs
Yurika-Kan Oct 27, 2026
4da10e2
Merge branch 'main' into yk/mspca-21-bulk-notifications
Yurika-Kan Oct 27, 2026
23e608f
docs(infra): correct known gaps for invalid and duplicate emails
Yurika-Kan Oct 27, 2026
f5ef851
Merge branch 'yk/mspca-21-bulk-notifications' of https://github.com/C…
Yurika-Kan Oct 27, 2026
46f3ff5
test(notifications): stub jwks-rsa in module spec
Yurika-Kan Oct 27, 2026
2f87513
docs(infra): lead readme with using the existing dev setup
Yurika-Kan Oct 27, 2026
dc3227b
docs(infra): drop tunnel options, dev lambda points at owner's tunnel
Yurika-Kan Oct 27, 2026
2356370
docs(infra): cut readme down to the essentials
Yurika-Kan Oct 27, 2026
88c97f7
addressed comments
Yurika-Kan Oct 28, 2026
3ef491a
merge main into yk/mspca-21-bulk-notifications
Yurika-Kan Nov 8, 2026
912b7ac
refactor(notifications): move dispatch lambda under aws/scheduler and…
Yurika-Kan Nov 8, 2026
f08fe98
merge main into yk/mspca-21-bulk-notifications
Yurika-Kan Nov 19, 2026
a87672c
refactor(notifications): move scheduler client factory under aws/sche…
Yurika-Kan Nov 19, 2026
6596e37
fix(notifications): return 500 when eventbridge scheduler fails to cr…
Yurika-Kan Nov 19, 2026
3d62094
fix(notifications): reject null filters and sendAt in bulk notificati…
Yurika-Kan Nov 19, 2026
c570aac
test(notifications): cover null and omitted filters and sendAt in bul…
Yurika-Kan Nov 19, 2026
65a66c6
feat(notifications): log scheduled dispatches dropped after retries
Yurika-Kan Nov 22, 2026
053e9fe
fix(notifications): truncate dispatch error body to 500 chars
Yurika-Kan Nov 22, 2026
03e8f11
docs(notifications): clarify bcc batch and inactive volunteer comments
Yurika-Kan Nov 22, 2026
1c28be9
refactor(notifications): drop redundant bcc email length check
Yurika-Kan Nov 22, 2026
8017bbf
refactor(notifications): move schedule creation into aws/scheduler se…
Yurika-Kan Nov 22, 2026
ea0de3f
refactor(notifications): drop redundant sender email check
Yurika-Kan Nov 22, 2026
2caea5c
feat(notifications): say why the dispatch guard rejects a request
Yurika-Kan Nov 22, 2026
2390f0a
refactor(notifications): drop recipient dedupe and email filter
Yurika-Kan Nov 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RecommendationsModule } from './recommendations/recommendations.module'
import { MatchesModule } from './matches/matches.module';
import { CoordinatorsModule } from './coordinators/coordinators.module';
import { VolunteersModule } from './volunteers/volunteers.module';
import { NotificationsModule } from './notifications/notifications.module';
import { OwnersModule } from './owners/owners.module';
import { AnimalsModule } from './animals/animals.module';

Expand All @@ -26,6 +27,7 @@ import { AnimalsModule } from './animals/animals.module';
MatchesModule,
CoordinatorsModule,
VolunteersModule,
NotificationsModule,
OwnersModule,
AnimalsModule,
],
Expand Down
26 changes: 26 additions & 0 deletions apps/backend/src/aws/scheduler/awsSchedulerClient.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Provider } from '@nestjs/common';
Comment thread
Yurika-Kan marked this conversation as resolved.
Resolved
import { SchedulerClient } from '@aws-sdk/client-scheduler';

export const AMAZON_SCHEDULER_CLIENT = 'AMAZON_SCHEDULER_CLIENT';

/**
* Factory that produces a new instance of the EventBridge Scheduler client.
* Reads region and credentials from process.env at injection time and
* passes them explicitly to the client, matching the SES client factory.
*
* Unlike that factory there is no disabled branch returning a dummy client: the
* empty-string fallbacks already make this constructible with no AWS config, and
* NotificationsService rejects a scheduled send before touching the client when
* the target and role ARNs are unset.
*/
export const AmazonSchedulerClientFactory: Provider<SchedulerClient> = {
provide: AMAZON_SCHEDULER_CLIENT,
useFactory: () =>
new SchedulerClient({
region: process.env.AWS_REGION ?? '',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? '',
},
}),
};
149 changes: 149 additions & 0 deletions apps/backend/src/aws/scheduler/lambda/notif/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Scheduled notifications: AWS setup

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this README can also go into the new aws folder


`POST /notifications` with `sendAt` doesn't send right away. It creates a one-time AWS schedule, and AWS calls the backend back at that time.

```
immediate: POST /notifications --> sendNow() --> SES
scheduled: POST /notifications --> Scheduler (waits until sendAt) --> Lambda --> POST /notifications/send-scheduled --> sendNow() --> SES
```

- Recipients are looked up at send time, so volunteers who went inactive in between are left out.
- The schedule deletes itself after it fires.
- Code: [notifications/](../../../../notifications) · [index.mjs](./index.mjs)
- `index.mjs` is deployed to Lambda on its own (see "Create from scratch"). It is not imported by the backend and not part of its bundle.

## Resources (dev, `us-east-2`)

| Resource | Name | Purpose |
| ----------- | ------------------------------ | ---------------------------------------------------------------------------------- |
| Lambda | `mspca-notif-dispatch` | Node 22, 30s timeout, 2 async retries. Forwards the schedule to `/send-scheduled`. |
| Lambda | `mspca-notif-dispatch-failed` | On-failure destination of the above. Logs `Dispatch gave up` with the attempt count. |
| IAM role | `mspca-notif-lambda-role` | Runs both Lambdas (CloudWatch logs, dispatch invokes the failure logger) |
| IAM role | `mspca-notif-scheduler-role` | Lets Scheduler invoke only this Lambda |
| User policy | `mspca-notif-create-schedules` | Lets the backend's IAM user create schedules and pass the scheduler role |

## Connect to the existing dev setup

Nothing to create. The backend won't boot without these 3 in `.env`. Ask the ticket owner by DM, and never commit them:

```
AWS_NOTIFICATION_DISPATCH_LAMBDA_ARN=
AWS_SCHEDULER_ROLE_ARN=
NOTIFICATIONS_DISPATCH_SECRET= # same value as the Lambda's
```

> The dev Lambda calls the **owner's ngrok tunnel** (`ngrok http 3000`). Any scheduled send, from anyone's laptop, fires into the owner's local backend. Test the full chain on the owner's machine. This goes away once the backend is deployed.

## Testing

Run `yarn serve:backend` with `AUTH_DISABLED=true`. In another tab, set `AWS_REGION=us-east-2`, `DISPATCH_SECRET` (your `.env` value) and `DISPATCH_URL`: the ngrok URL on the owner's machine, or `http://localhost:3000/api/notifications/send-scheduled` for everyone else.

| # | Test | Command | Expect | Dev run |
| --- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | ------- |
| T0 | No secret | `curl -i -X POST ${DISPATCH_URL}` | 401 | ✅ |
| T1 | Right secret, empty body | T0 + `-H "x-notifications-dispatch-secret: ${DISPATCH_SECRET}" -H 'content-type: application/json' -d '{}'` | 400 | ✅ |
| T2 | Wrong secret | T1 with secret `wrong` and a valid body | 401, no email | ✅ |
| T3 | Send now | `curl -i -X POST localhost:3000/api/notifications -H 'content-type: application/json' -d '{"subject":"t3","body":"<p>t3</p>","filters":["Dog"]}'` | 202, only Dog volunteers get it | ✅ |
| T4 | Lambda --> backend (owner) | `aws lambda invoke --region ${AWS_REGION} --function-name mspca-notif-dispatch --cli-binary-format raw-in-base64-out --payload '{"subject":"t4","body":"<p>t4</p>","filters":["Cat"]}' /tmp/out.json` | `StatusCode 200`, `null` | ✅ |
| T5 | Full chain (owner) | T3 with `"filters":[]` and `"sendAt":"$(date -u -v+2M +%Y-%m-%dT%H:%M:%SZ)"` | 202, all active volunteers get it about 2 min later | ✅ |
| T6 | Retries (owner) | Stop the backend, run T4 with `--invocation-type Event`, then `aws logs tail /aws/lambda/mspca-notif-dispatch --since 10m` and `aws logs tail /aws/lambda/mspca-notif-dispatch-failed --since 10m` | 3 `Dispatch failed` lines, then 1 `Dispatch gave up ... after 3 attempts` in the `-failed` log, no email | ✅ |
| T7 | Validation | past `sendAt`, `filters:["Horse"]`, missing field, line break in subject | 400 | ✅ |

The dev run was 2026-10-26/27 (T6 rerun 2026-11-19). Unit tests: `npx jest apps/backend/src/notifications`.

## Create from scratch (prod / rebuild)

Only for a new environment or if the dev resources are deleted. For prod, use new names and a new secret. You need a CLI login with IAM and Lambda admin rights.

> **zsh:** write `${VAR}` with braces. `$ACCOUNT_ID:role` gets eaten by zsh's `:r` modifier.

```bash
# 0. shell vars (this tab only; save the secret)
export AWS_REGION=us-east-2
export ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
export DISPATCH_SECRET=$(openssl rand -hex 32)
export DISPATCH_URL=https://<api host>/api/notifications/send-scheduled

# 1. lambda role
aws iam create-role --role-name mspca-notif-lambda-role \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws iam attach-role-policy --role-name mspca-notif-lambda-role \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
sleep 10 # let the role propagate

# 2. lambda
zip -j /tmp/notif-dispatch.zip apps/backend/src/aws/scheduler/lambda/notif/index.mjs
aws lambda create-function --region ${AWS_REGION} --function-name mspca-notif-dispatch \
--runtime nodejs22.x --handler index.handler --timeout 30 \
--zip-file fileb:///tmp/notif-dispatch.zip \
--role arn:aws:iam::${ACCOUNT_ID}:role/mspca-notif-lambda-role \
--environment "Variables={NOTIFICATIONS_DISPATCH_URL=${DISPATCH_URL},NOTIFICATIONS_DISPATCH_SECRET=${DISPATCH_SECRET}}"
aws lambda put-function-event-invoke-config --region ${AWS_REGION} \
--function-name mspca-notif-dispatch --maximum-retry-attempts 2

# 2b. failure logger: gets a record when dispatch runs out of retries, logs "Dispatch gave up"
zip -j /tmp/notif-failed.zip apps/backend/src/aws/scheduler/lambda/notif/failed.mjs
aws lambda create-function --region ${AWS_REGION} --function-name mspca-notif-dispatch-failed \
--runtime nodejs22.x --handler failed.handler --timeout 10 \
--zip-file fileb:///tmp/notif-failed.zip \
--role arn:aws:iam::${ACCOUNT_ID}:role/mspca-notif-lambda-role
aws lambda put-function-event-invoke-config --region ${AWS_REGION} \
--function-name mspca-notif-dispatch-failed --maximum-retry-attempts 0
aws iam put-role-policy --role-name mspca-notif-lambda-role --policy-name invoke-notif-dispatch-failed \
--policy-document "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:${AWS_REGION}:${ACCOUNT_ID}:function:mspca-notif-dispatch-failed\"}]}"
sleep 10
aws lambda put-function-event-invoke-config --region ${AWS_REGION} \
--function-name mspca-notif-dispatch --maximum-retry-attempts 2 \
--destination-config "{\"OnFailure\":{\"Destination\":\"arn:aws:lambda:${AWS_REGION}:${ACCOUNT_ID}:function:mspca-notif-dispatch-failed\"}}"

# 3. scheduler role
aws iam create-role --role-name mspca-notif-scheduler-role \
--assume-role-policy-document "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"scheduler.amazonaws.com\"},\"Action\":\"sts:AssumeRole\",\"Condition\":{\"StringEquals\":{\"aws:SourceAccount\":\"${ACCOUNT_ID}\"}}}]}"
aws iam put-role-policy --role-name mspca-notif-scheduler-role --policy-name invoke-notif-dispatch \
--policy-document "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:${AWS_REGION}:${ACCOUNT_ID}:function:mspca-notif-dispatch\"}]}"

# 4. backend IAM user (the one behind .env's AWS key)
export BACKEND_USER=$( (set -a; source .env; aws sts get-caller-identity --query Arn --output text) | sed 's|.*/||' )
aws iam put-user-policy --user-name ${BACKEND_USER} --policy-name mspca-notif-create-schedules \
--policy-document "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"scheduler:CreateSchedule\",\"Resource\":\"arn:aws:scheduler:${AWS_REGION}:${ACCOUNT_ID}:schedule/default/notification-*\"},{\"Effect\":\"Allow\",\"Action\":\"iam:PassRole\",\"Resource\":\"arn:aws:iam::${ACCOUNT_ID}:role/mspca-notif-scheduler-role\",\"Condition\":{\"StringEquals\":{\"iam:PassedToService\":\"scheduler.amazonaws.com\"}}}]}"

# 5. backend env: paste into .env or the host's config, then restart
echo "AWS_NOTIFICATION_DISPATCH_LAMBDA_ARN=arn:aws:lambda:${AWS_REGION}:${ACCOUNT_ID}:function:mspca-notif-dispatch"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add step by step instructions for where to get each of these env variables here?

echo "AWS_SCHEDULER_ROLE_ARN=arn:aws:iam::${ACCOUNT_ID}:role/mspca-notif-scheduler-role"
echo "NOTIFICATIONS_DISPATCH_SECRET=${DISPATCH_SECRET}"
```

To change the Lambda's URL or secret later, run `aws lambda update-function-configuration` with the same `--environment` as step 2. After editing `index.mjs`, rezip and run `aws lambda update-function-code --function-name mspca-notif-dispatch --zip-file fileb:///tmp/notif-dispatch.zip`.

## Prod checklist

- [ ] Add `@Public()` to `/send-scheduled`. The Cognito guard is global, and the Lambda has no token, so it gets a 401 once auth is on.
- [ ] Deploy the backend, point the Lambda's `NOTIFICATIONS_DISPATCH_URL` at it, and put the step 5 vars in the host's config.
- [ ] Use separate prod resources and a new secret, so dev tests can't email real volunteers.
- [ ] Send from a verified domain (for example `c4cneu.com`), not Gmail. Gmail sends land in spam.
- [ ] Follow-ups: failed SES batches aren't retried (only logged); coordinator-only role gating; `ArrayOverlap()` after [#11](https://github.com/Code-4-Community/mspca/pull/11).

## Troubleshooting

| Symptom | Fix |
| ----------------------------------------------- | --------------------------------------------------------------------------------------- |
| ARN regex error like `...177ole/` | zsh `:r` modifier. Use `${VAR}` braces. |
| `POST /notifications` with `sendAt` returns 500 | Scheduler env vars unset, or step 4 `iam:PassRole` missing. Check the backend log. |
| Lambda log `Dispatch failed: 401` | Secrets differ between `.env` and the Lambda (in prod, also the `@Public()` item above) |
| Lambda log `Dispatch failed: 404` / `502` | Wrong URL path, or the tunnel/backend is down |
| No Lambda logs at all | Scheduler can't invoke it. Check the step 3 role and the ARN in `.env`. |

## Cleanup

```bash
aws lambda delete-function --region ${AWS_REGION} --function-name mspca-notif-dispatch
aws lambda delete-function --region ${AWS_REGION} --function-name mspca-notif-dispatch-failed
aws iam delete-role-policy --role-name mspca-notif-lambda-role --policy-name invoke-notif-dispatch-failed
aws iam detach-role-policy --role-name mspca-notif-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name mspca-notif-lambda-role
aws iam delete-role-policy --role-name mspca-notif-scheduler-role --policy-name invoke-notif-dispatch
aws iam delete-role --role-name mspca-notif-scheduler-role
aws iam delete-user-policy --user-name ${BACKEND_USER} --policy-name mspca-notif-create-schedules
```

Refs: [Scheduler one-time schedules](https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#one-time) · [Scheduler execution role](https://docs.aws.amazon.com/scheduler/latest/UserGuide/setting-up.html#setting-up-execution-role) · [Lambda async retries](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html) · [`iam:PassRole`](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html)
14 changes: 14 additions & 0 deletions apps/backend/src/aws/scheduler/lambda/notif/failed.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* On-failure destination for mspca-notif-dispatch.
*
* Lambda sends this a record once the dispatch Lambda runs out of async retries,
* so a dropped scheduled notification shows up in this function's logs with its
* attempt count and original payload. Only logs, never throws, so nothing retries.
*/
export const handler = async (event) => {
const { requestId, condition, approximateInvokeCount } = event.requestContext ?? {};
console.error(
`Dispatch gave up (request ${requestId}): ${condition} after ${approximateInvokeCount} attempts`,
JSON.stringify(event),
);
};
40 changes: 40 additions & 0 deletions apps/backend/src/aws/scheduler/lambda/notif/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
Comment thread
Yurika-Kan marked this conversation as resolved.
Resolved
* EventBridge Scheduler target for scheduled bulk notifications.
*
* Carries no business logic. Scheduler passes the notification stored in the
* schedule's Input as this handler's event, and the handler forwards it to the
* backend, which resolves the recipients at send time and sends the email.
*
* Environment:
* NOTIFICATIONS_DISPATCH_URL https://<api host>/api/notifications/send-scheduled
* NOTIFICATIONS_DISPATCH_SECRET must match the backend's value
*/
export const handler = async (event, context) => {
const url = process.env.NOTIFICATIONS_DISPATCH_URL;
const secret = process.env.NOTIFICATIONS_DISPATCH_SECRET;

if (!url || !secret) {
throw new Error(
'NOTIFICATIONS_DISPATCH_URL and NOTIFICATIONS_DISPATCH_SECRET must both be set',
);
}

const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-notifications-dispatch-secret': secret,
},
body: JSON.stringify(event),
});

// Scheduler invokes this Lambda asynchronously, so throwing triggers Lambda's async set 2 default
// Lambda doesn't pass attempt number, but request ID stays the same across retries so count those
if (!response.ok) {
throw new Error(
`Dispatch failed (request ${context.awsRequestId}): ${
response.status
} ${(await response.text()).slice(0, 500)}`,
);
}
};
11 changes: 11 additions & 0 deletions apps/backend/src/aws/scheduler/scheduler.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { SchedulerService } from './scheduler.service';
import { AmazonSchedulerClientFactory } from './awsSchedulerClient.factory';

// No env check here: NotificationsModule, the only consumer, validates the AWS vars
// it needs at boot, and the client factory is constructible without them.
@Module({
providers: [AmazonSchedulerClientFactory, SchedulerService],
exports: [SchedulerService],
})
export class AWSSchedulerModule {}
55 changes: 55 additions & 0 deletions apps/backend/src/aws/scheduler/scheduler.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SchedulerService } from './scheduler.service';
import { AMAZON_SCHEDULER_CLIENT } from './awsSchedulerClient.factory';

describe('SchedulerService', () => {
let service: SchedulerService;
let scheduler: { send: jest.Mock };

const input = {
name: 'test-schedule',
at: new Date('2030-01-01T09:30:00.000Z'),
targetArn: 'arn:aws:lambda:::fn',
roleArn: 'arn:aws:iam:::role/scheduler',
input: { hello: 'world' },
retryPolicy: { MaximumRetryAttempts: 1, MaximumEventAgeInSeconds: 60 },
};

beforeEach(async () => {
scheduler = { send: jest.fn().mockResolvedValue({}) };

const module: TestingModule = await Test.createTestingModule({
providers: [
SchedulerService,
{ provide: AMAZON_SCHEDULER_CLIENT, useValue: scheduler },
],
}).compile();

service = module.get<SchedulerService>(SchedulerService);
});

it('creates a one-time, self-deleting UTC schedule for the target', async () => {
await service.scheduleOnce(input);

expect(scheduler.send).toHaveBeenCalledTimes(1);
expect(scheduler.send.mock.calls[0][0].input).toEqual({
Name: 'test-schedule',
ScheduleExpression: 'at(2030-01-01T09:30:00)',
ScheduleExpressionTimezone: 'UTC',
FlexibleTimeWindow: { Mode: 'OFF' },
ActionAfterCompletion: 'DELETE',
Target: {
Arn: 'arn:aws:lambda:::fn',
RoleArn: 'arn:aws:iam:::role/scheduler',
RetryPolicy: { MaximumRetryAttempts: 1, MaximumEventAgeInSeconds: 60 },
Input: '{"hello":"world"}',
},
});
});

it('passes a Scheduler rejection through to the caller', async () => {
scheduler.send.mockRejectedValueOnce(new Error('AccessDenied'));

await expect(service.scheduleOnce(input)).rejects.toThrow('AccessDenied');
});
});
Loading
Loading