Skip to content
Next Next commit
adding in billing changes
  • Loading branch information
colerogers committed Jan 19, 2022
commit 632bd507fb07ccd13215daf1292298064d2cb059
126 changes: 126 additions & 0 deletions spec/v2/providers/alerts/billing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { expect } from 'chai';
import * as alerts from '../../../../src/v2/providers/alerts';
import * as billing from '../../../../src/v2/providers/alerts/billing';
import { BASIC_ENDPOINT, BASIC_OPTIONS } from '../helpers';

const ALERT_TYPE = 'new-alert-type';
const myHandler = () => 42;

describe('billing', () => {
describe('onPlanUpdatePublished', () => {
it('should create a function with only handler', () => {
const func = billing.onPlanUpdatePublished(myHandler);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: billing.planUpdateAlert,
},
retry: false,
},
});
});

it('should create a function with opts & handler', () => {
const func = billing.onPlanUpdatePublished(
{ ...BASIC_OPTIONS },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...BASIC_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: billing.planUpdateAlert,
},
retry: false,
},
});
});
});

describe('onAutomatedPlanUpdatePublished', () => {
it('should create a function with only handler', () => {
const func = billing.onAutomatedPlanUpdatePublished(myHandler);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: billing.automatedPlanUpdateAlert,
},
retry: false,
},
});
});

it('should create a function with opts & handler', () => {
const func = billing.onAutomatedPlanUpdatePublished(
{ ...BASIC_OPTIONS },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...BASIC_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: billing.automatedPlanUpdateAlert,
},
retry: false,
},
});
});
});

describe('onOperation', () => {
it('should create a function with alertType only', () => {
const func = billing.onOperation(ALERT_TYPE, myHandler, undefined);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should create a function with opts', () => {
const func = billing.onOperation(
ALERT_TYPE,
{ ...BASIC_OPTIONS },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...BASIC_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should create a function with a run method', () => {
const func = billing.onOperation(ALERT_TYPE, (event) => event, undefined);

const res = func.run('input' as any);

expect(res).to.equal('input');
});
});
});
105 changes: 105 additions & 0 deletions src/v2/providers/alerts/billing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { defineEndpoint, FirebaseAlertData } from '.';
import { ManifestEndpoint } from '../../../common/manifest';
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';

/** Data */
// billing.planUpdate
export interface PlanUpdatePayload {
['@type']: 'com.google.firebase.firebasealerts.PlanUpdatePayload';
billingPlan: string;
principalEmail: string;
}
// billing.automatedPlanUpdate
export interface PlanAutomatedUpdatePayload {
['@type']: 'com.google.firebase.firebasealerts.PlanAutomatedUpdatePayload';
billingPlan: string;
}

/** Events */
/** @internal */
export const planUpdateAlert = 'billing.planUpdate';
/** @internal */
export const automatedPlanUpdateAlert = 'billing.automatedPlanUpdate';

/** Cloud Event Type */
interface WithAlertType {
alertType: string;
}
export type BillingEvent<T> = CloudEvent<FirebaseAlertData<T>, WithAlertType>;

/** Handlers */
export function onPlanUpdatePublished(
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
opts: options.EventHandlerOptions,
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>),
handler?: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>> {
return onOperation<PlanUpdatePayload>(
planUpdateAlert,
optsOrHandler,
handler
);
}

export function onAutomatedPlanUpdatePublished(
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
export function onAutomatedPlanUpdatePublished(
opts: options.EventHandlerOptions,
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
export function onAutomatedPlanUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanAutomatedUpdatePayload>) => any | Promise<any>),
handler?: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>> {
return onOperation<PlanAutomatedUpdatePayload>(
automatedPlanUpdateAlert,
optsOrHandler,
handler
);
}

/** @internal */
export function onOperation<T>(
alertType: string,
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<T>) => any | Promise<any>),
handler: (event: BillingEvent<T>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<T>> {
if (typeof optsOrHandler === 'function') {
handler = optsOrHandler as (event: BillingEvent<T>) => any | Promise<any>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Woo (event: BillingEvent<T>) => any | Promise<any>; is used thrice! This meets the threshold for DRY for me:

type BillingEventHandler<T> = (event: BillingEvent<T>) => any | Promise<any>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that looks way better, thanks for the suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@taeold Removed based on typing feedback below

optsOrHandler = {};
}

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as BillingEvent<T>);
};

func.run = handler;

// TypeScript doesn't recognize defineProperty as adding a property and complains
// that __endpoint doesn't exist. We can either cast to any and lose all type safety
// or we can just assign a meaningless value before calling defineProperty.
func.__trigger = 'silence the transpiler';
func.__endpoint = {} as ManifestEndpoint;
defineEndpoint(func, optsOrHandler, alertType, undefined);

return func;
}
26 changes: 26 additions & 0 deletions v2/alerts/billing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The MIT License (MIT)
//
// Copyright (c) 2021 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// This file is not part of the firebase-functions SDK. It is used to silence the
// imports eslint plugin until it can understand import paths defined by node
// package exports.
// For more information, see github.com/import-js/eslint-plugin-import/issues/1810