Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/11559 newsletter scheduler common package #1679

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions packages/common/src/newsletters-scheduler/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./subscriptions";
export * from "./types";
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* tslint:disable:interface-over-type-literal */
import { Awaited } from "../awaited-type";

/**
* Generated by orval v6.24.0 🍺
* Do not edit manually.
* Hub Newsletters Scheduler
* OpenAPI spec version: 0.0.1
*/
import { customClient } from "../custom-client";
export type ISubscriptionMetadata = { [key: string]: any };

export type ISubscriptionCatalog = { [key: string]: any } | null;

export interface IUser {
agoId: string;
createdAt: string;
deleted: boolean;
email: string;
firstName: string;
lastName: string;
optedOut: boolean;
updatedAt: string;
username: string;
}

export interface INotificationSpec {
createdAt: string;
createdById: string;
description: string;
id: number;
name: string;
updatedAt: string;
}

export enum DeliveryMethod {
EMAIL = "EMAIL",
}
export interface ISubscription {
actions: SubscriptionActions[];
active: boolean;
cadence: Cadence;
catalog?: ISubscriptionCatalog;
createdAt: string;
deliveryMethod: DeliveryMethod;
entityId: string;
entityType: SubscriptionEntityType;
id: number;
lastDelivery: string;
metadata: ISubscriptionMetadata;
notificationSpec?: INotificationSpec;
notificationSpecId: number;
updatedAt: string;
user?: IUser;
userId: string;
}

export enum SystemNotificationSpecNames {
TELEMETRY_REPORT = "TELEMETRY_REPORT",
EVENT = "EVENT",
DISCUSSION_ON_ENTITY = "DISCUSSION_ON_ENTITY",
}
export enum SubscriptionEntityType {
DISCUSSION = "DISCUSSION",
}
export enum Cadence {
ON_EVENT = "ON_EVENT",
DAILY = "DAILY",
WEEKLY = "WEEKLY",
MONTHLY = "MONTHLY",
}
export enum SubscriptionActions {
DISCUSSION_POST_PENDING = "DISCUSSION_POST_PENDING",
}
export interface INotify {
/** An array of actions representing user selections that further customize the subscription behavior */
actions?: SubscriptionActions[];
/** Frequency of the subscription */
cadence: Cadence;
/** The AGO id of the entity associated with the subscription */
entityId?: string;
/** The type of entity associated with the subscription entityId */
entityType?: SubscriptionEntityType;
/** Notification spec name for the subscription */
notificationSpecName: SystemNotificationSpecNames;
}

type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];

export const notify = (
iNotify: INotify,
options?: SecondParameter<typeof customClient>
) => {
return customClient<ISubscription[]>(
{
url: `/api/newsletters-scheduler/v1/subscriptions/notify`,
method: "POST",
headers: { "Content-Type": "application/json" },
data: iNotify,
},
options
);
};

export type NotifyResult = NonNullable<Awaited<ReturnType<typeof notify>>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Orval generates return types for functions using utility type Awaited
* This was introduced in Typescript 4.5, but hub.js is using Typescript 3
*/
export type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Generated and copied from the [hub engagement repo](https://github.com/ArcGIS/hub-newsletters/blob/master/orval/custom-client.ts)
* Do not edit manually
*/
export interface IOrvalParams {
url: string;
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
headers?: HeadersInit;
params?: Record<string, any>; // query params
data?: Record<string, any>; // request body
}

export interface ICustomParams {
hubApiUrl?: string;
token?: string;
headers?: HeadersInit;
params?: Record<string, any>; // query params
data?: Record<string, any>; // request body
mode?: RequestMode;
cache?: RequestCache;
credentials?: RequestCredentials;
}

export async function customClient<T>(
orvalParams: IOrvalParams,
customParams: ICustomParams
): Promise<T> {
const { url, method, data } = orvalParams;
const { mode, cache, credentials } = customParams;
const { headers, params } = combineParams(orvalParams, customParams);

const baseUrl = removeTrailingSlash(customParams.hubApiUrl);
const requestUrl = `${baseUrl}${url}?${new URLSearchParams(params)}`;

const requestOptions: RequestInit = {
headers,
method,
cache,
credentials,
mode,
};
if (data) {
requestOptions.body = JSON.stringify(data);
}

const res = await fetch(requestUrl, requestOptions);
const { statusText, status } = res;

if (res.ok) {
return res.json();
}

const error = await res.json();
throw new RemoteServerError(
statusText,
requestUrl,
status,
JSON.stringify(error.message)
);
}

function removeTrailingSlash(hubApiUrl = "https://hub.arcgis.com") {
return hubApiUrl.replace(/\/$/, "");
}

function combineParams(orvalParams: IOrvalParams, options: ICustomParams) {
const headers = new Headers({
...orvalParams.headers,
...options.headers,
});
if (options.token) {
headers.set("Authorization", options.token);
}

const params = {
...orvalParams.params,
...options.params,
};

return { headers, params };
}

class RemoteServerError extends Error {
status: number;
url: string;
error: string;

constructor(message: string, url: string, status: number, error: string) {
super(message);
this.status = status;
this.url = url;
this.error = error;
}
}
17 changes: 17 additions & 0 deletions packages/common/src/newsletters-scheduler/api/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { INotifyParams } from "./types";
import { authenticateRequest } from "./utils/authenticate-request";
import {
notify as _notify,
ISubscription,
} from "./orval/api/orval-newsletters-scheduler";

/**
* Notify (schedule) subscriptions to recipients
*
* @param {INotifyParams} options
* @return {Promise<ISubscription[]>}
*/
export async function notify(options: INotifyParams): Promise<ISubscription[]> {
options.token = await authenticateRequest(options);
return _notify(options.data, options);
}
37 changes: 37 additions & 0 deletions packages/common/src/newsletters-scheduler/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export {
ISubscriptionMetadata,
ISubscriptionCatalog,
IUser,
INotificationSpec,
DeliveryMethod,
ISubscription,
SystemNotificationSpecNames,
SubscriptionEntityType,
Cadence,
SubscriptionActions,
INotify,
} from "./orval/api/orval-newsletters-scheduler";
import { IHubRequestOptions } from "../../types";
import { INotify } from "./orval/api/orval-newsletters-scheduler";

/**
* options for making requests against the Newsletters Scheduler API
*
* @export
* @interface INewslettersSchedulerRequestOptions
* @extends IHubRequestOptions
*/
export interface INewslettersSchedulerRequestOptions
extends Omit<IHubRequestOptions, "httpMethod" | "isPortal">,
Pick<RequestInit, "mode" | "cache" | "credentials"> {
httpMethod?: "GET" | "POST" | "PATCH" | "DELETE";
isPortal?: boolean;
token?: string;
data?: {
[key: string]: any;
};
}

export interface INotifyParams extends INewslettersSchedulerRequestOptions {
data: INotify;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { INewslettersSchedulerRequestOptions } from "../types";

/**
* return a token created using options.authentication or set on options.token
*
* @export
* @param {INewslettersSchedulerRequestOptions} options
* @return {*} {Promise<string>}
*/
export function authenticateRequest(
options: INewslettersSchedulerRequestOptions
): Promise<string> {
const { token, authentication } = options;

if (authentication) {
return authentication.getToken(authentication.portal);
}

return Promise.resolve(token);
}
1 change: 1 addition & 0 deletions packages/common/src/newsletters-scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./api";
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
ISubscription,
Cadence,
SystemNotificationSpecNames,
INotifyParams,
notify,
SubscriptionEntityType,
SubscriptionActions,
} from "../../../src/newsletters-scheduler";
import * as authenticateRequestModule from "../../../src/newsletters-scheduler/api/utils/authenticate-request";
import * as orvalModule from "../../../src/newsletters-scheduler/api/orval/api/orval-newsletters-scheduler";

describe("Subscriptions", () => {
const token = "aaa";
let authenticateRequestSpy: any;

beforeEach(() => {
authenticateRequestSpy = spyOn(
authenticateRequestModule,
"authenticateRequest"
).and.callFake(async () => token);
});

describe("/subscriptions/notify", () => {
it("should notify", async () => {
const mockSubscription = {
subscription: "mock",
} as unknown as ISubscription[];
const notifySpy = spyOn(orvalModule, "notify").and.callFake(
async () => mockSubscription
);

const options: INotifyParams = {
data: {
actions: [SubscriptionActions.DISCUSSION_POST_PENDING],
cadence: Cadence.WEEKLY,
notificationSpecName:
SystemNotificationSpecNames.DISCUSSION_ON_ENTITY,
entityId: "burrito",
entityType: SubscriptionEntityType.DISCUSSION,
},
};

const result = await notify(options);
expect(result).toEqual(mockSubscription);

expect(authenticateRequestSpy).toHaveBeenCalledWith(options);
expect(notifySpy).toHaveBeenCalledWith(options.data, {
...options,
token,
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IAuthenticationManager } from "@esri/arcgis-rest-request";
import { INewslettersSchedulerRequestOptions } from "../../../../src/newsletters-scheduler";
import { authenticateRequest } from "../../../../src/newsletters-scheduler/api/utils/authenticate-request";

describe("authenticateRequest", () => {
let getTokenSpy: any;

const portal = "https://foo.com";
const token = "aaa";
const authentication = {
portal,
async getToken() {
return token;
},
} as IAuthenticationManager;

beforeEach(() => {
getTokenSpy = spyOn(authentication, "getToken").and.callThrough();
});

it("returns params.token if provided", async () => {
const options: INewslettersSchedulerRequestOptions = { token: "bbb" };

const result = await authenticateRequest(options);
expect(result).toEqual("bbb");
expect(getTokenSpy).not.toHaveBeenCalled();
});

it("returns token from authentication if params.token not provided", async () => {
const options = { authentication } as INewslettersSchedulerRequestOptions;

const result = await authenticateRequest(options);
expect(result).toEqual(token);
expect(getTokenSpy).toHaveBeenCalledWith(portal);
});
});
Loading