Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions libs/accounts/email-sender/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions libs/accounts/email-sender/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# accounts-email-sender

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build accounts-email-senders` to build the library.

## Running unit tests

Run `nx test-unit accounts-email-senders` to execute the unit tests via [Jest](https://jestjs.io).
21 changes: 21 additions & 0 deletions libs/accounts/email-sender/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable */
export default {
displayName: 'accounts-email-sender',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/accounts/email-sender',
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: 'artifacts/tests/lib/accounts/email-sender',
outputName: 'accounts-email-sender-jest-unit-results.xml',
},
],
],
};
9 changes: 9 additions & 0 deletions libs/accounts/email-sender/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@fxa/accounts/email-sender",
"version": "0.0.1",
"dependencies": {},
"type": "commonjs",
"main": "./index.cjs",
"types": "./index.d.ts",
"private": true
}
37 changes: 37 additions & 0 deletions libs/accounts/email-sender/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "accounts-email-sender",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/accounts/email-sender/src",
"projectType": "library",
"tags": ["scope:shared:lib"],
"targets": {
"build": {
"executor": "@nx/esbuild:esbuild",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/accounts/email-sender",
"main": "libs/accounts/email-sender/src/index.ts",
"tsConfig": "libs/accounts/email-sender/tsconfig.lib.json",
"assets": ["libs/accounts/email-sender/*.md"],
"format": ["cjs"],
"generatePackageJson": true
}
},
"test-unit": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/accounts/email-sender/jest.config.ts",
"testPathPattern": ["^(?!.*\\.in\\.spec\\.ts$).*$"]
}
},
"test-integration": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/accounts/email-sender/jest.config.ts",
"testPathPattern": ["\\.in\\.spec\\.ts$"]
}
}
}
}
106 changes: 106 additions & 0 deletions libs/accounts/email-sender/src/bounces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { AppError } from '@fxa/accounts/errors';

export type BouncesConfig = {
enabled: boolean;
hard: Record<number, number>;
soft: Record<number, number>;
complaint: Record<number, number>;
ignoreTemplates: string[];
};

export type Bounce = {
bounceType: number;
createdAt: number;
};

export type Tally = {
count: number;
latest: number;
};

export type BounceDb = {
emailBounces: {
findByEmail(email: string): Promise<Array<Bounce>>;
};
};

const BOUNCE_TYPE_HARD = 1;
const BOUNCE_TYPE_SOFT = 2;
const BOUNCE_TYPE_COMPLAINT = 3;

export class Bounces {
private readonly bounceRules: Record<number, any>;

constructor(
private readonly config: BouncesConfig,
private readonly db: BounceDb
) {
this.bounceRules = {
[BOUNCE_TYPE_HARD]: Object.freeze(config.hard || {}),
[BOUNCE_TYPE_SOFT]: Object.freeze(config.soft || {}),
[BOUNCE_TYPE_COMPLAINT]: Object.freeze(config.complaint || {}),
};
}

async check(email: string, template: string) {
if (this.config.enabled) {
return await this.checkBounces(email, template);
}
}

async checkBounces(email: string, template: string) {
if (this.config.ignoreTemplates.includes(template)) {
return;
}

const bounces = await this.db.emailBounces.findByEmail(email);
this.applyRules(bounces);
}

private applyRules(bounces: Array<Bounce>) {
const tallies: Record<number, any> = {
[BOUNCE_TYPE_HARD]: {
count: 0,
latest: 0,
},
[BOUNCE_TYPE_COMPLAINT]: {
count: 0,
latest: 0,
},
[BOUNCE_TYPE_SOFT]: {
count: 0,
latest: 0,
},
};
const now = Date.now();

bounces.forEach((bounce: Bounce) => {
const type = bounce.bounceType;
const ruleSet = this.bounceRules[type];
if (ruleSet) {
const tally = tallies[type];
const tier = ruleSet[tally.count];
if (!tally.latest) {
tally.latest = bounce.createdAt;
}
if (tier && bounce.createdAt > now - tier) {
if (type === BOUNCE_TYPE_HARD) {
throw AppError.emailBouncedHard(tally.latest);
}
if (type === BOUNCE_TYPE_COMPLAINT) {
throw AppError.emailComplaint(tally.latest);
}
if (type === BOUNCE_TYPE_SOFT) {
throw AppError.emailBouncedSoft(tally.latest);
}
}
tally.count++;
}
});
return tallies;
}
}
Loading