Skip to content

Commit

Permalink
refactor: unify messages interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Dec 9, 2021
1 parent e16a211 commit 3deca80
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 163 deletions.
5 changes: 1 addition & 4 deletions src/__tests__/mockMessages.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Messages } from "../services";

export const newMockMessages = (): jest.Mocked<Messages> => ({
adminCreateUser: jest.fn(),
authentication: jest.fn(),
forgotPassword: jest.fn(),
signUp: jest.fn(),
create: jest.fn(),
});
27 changes: 18 additions & 9 deletions src/services/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ describe("messages service", () => {
});

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
const message = await messages.create(
TestContext,
"Authentication",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -67,8 +68,9 @@ describe("messages service", () => {
mockTriggers.customMessage.mockResolvedValue(null);

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
const message = await messages.create(
TestContext,
"Authentication",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -102,8 +104,9 @@ describe("messages service", () => {
mockTriggers.enabled.mockReturnValue(false);

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
const message = await messages.create(
TestContext,
"Authentication",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -136,8 +139,9 @@ describe("messages service", () => {
});

const messages = new MessagesService(mockTriggers);
const message = await messages.forgotPassword(
const message = await messages.create(
TestContext,
"ForgotPassword",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -176,8 +180,9 @@ describe("messages service", () => {
mockTriggers.customMessage.mockResolvedValue(null);

const messages = new MessagesService(mockTriggers);
const message = await messages.forgotPassword(
const message = await messages.create(
TestContext,
"ForgotPassword",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -211,8 +216,9 @@ describe("messages service", () => {
mockTriggers.enabled.mockReturnValue(false);

const messages = new MessagesService(mockTriggers);
const message = await messages.forgotPassword(
const message = await messages.create(
TestContext,
"ForgotPassword",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -245,8 +251,9 @@ describe("messages service", () => {
});

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
const message = await messages.create(
TestContext,
"SignUp",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -285,8 +292,9 @@ describe("messages service", () => {
mockTriggers.customMessage.mockResolvedValue(null);

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
const message = await messages.create(
TestContext,
"SignUp",
"clientId",
"userPoolId",
user,
Expand Down Expand Up @@ -320,8 +328,9 @@ describe("messages service", () => {
mockTriggers.enabled.mockReturnValue(false);

const messages = new MessagesService(mockTriggers);
const message = await messages.signUp(
const message = await messages.create(
TestContext,
"SignUp",
"clientId",
"userPoolId",
user,
Expand Down
138 changes: 17 additions & 121 deletions src/services/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,20 @@ export interface Message {
smsMessage?: string;
}

type MessageSource =
| "AdminCreateUser"
| "Authentication"
| "ForgotPassword"
| "ResendCode"
| "SignUp"
| "UpdateUserAttribute"
| "VerifyUserAttribute";

export interface Messages {
adminCreateUser(
ctx: Context,
userPoolId: string,
user: User,
temporaryPassword: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message>;
authentication(
ctx: Context,
clientId: string,
userPoolId: string,
user: User,
code: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message>;
forgotPassword(
create(
ctx: Context,
clientId: string,
userPoolId: string,
user: User,
code: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message>;
signUp(
ctx: Context,
clientId: string,
source: MessageSource,
clientId: string | null,
userPoolId: string,
user: User,
code: string,
Expand All @@ -52,112 +39,21 @@ export class MessagesService implements Messages {
this.triggers = triggers;
}

public async adminCreateUser(
ctx: Context,
userPoolId: string,
user: User,
temporaryPassword: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage(ctx, {
clientId: AWS_ADMIN_CLIENT_ID,
clientMetadata,
code: temporaryPassword,
source: "CustomMessage_AdminCreateUser",
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
});

return {
__code: temporaryPassword,
...message,
};
}

// TODO: What should the default message be?
return {
__code: temporaryPassword,
};
}

public async authentication(
ctx: Context,
clientId: string,
userPoolId: string,
user: User,
code: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage(ctx, {
clientId,
clientMetadata,
code,
source: "CustomMessage_Authentication",
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
});

return {
__code: code,
...message,
};
}

// TODO: What should the default message be?
return {
__code: code,
};
}

public async forgotPassword(
ctx: Context,
clientId: string,
userPoolId: string,
user: User,
code: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage(ctx, {
clientId,
clientMetadata,
code,
source: "CustomMessage_ForgotPassword",
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
});

return {
__code: code,
...message,
};
}

// TODO: What should the default message be?
return {
__code: code,
};
}

public async signUp(
public async create(
ctx: Context,
clientId: string,
source: MessageSource,
clientId: string | null,
userPoolId: string,
user: User,
code: string,
clientMetadata: Record<string, string> | undefined
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage(ctx, {
clientId,
clientId: clientId ?? AWS_ADMIN_CLIENT_ID,
clientMetadata,
code,
source: "CustomMessage_SignUp",
source: `CustomMessage_${source}`,
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
Expand Down
Loading

0 comments on commit 3deca80

Please sign in to comment.