Skip to content

Commit

Permalink
feat: added firebase scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Oct 3, 2022
1 parent 32638e3 commit 2114446
Show file tree
Hide file tree
Showing 27 changed files with 1,651 additions and 98 deletions.
21 changes: 20 additions & 1 deletion apps/demo-api/src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { demoCreateModel, demoDeleteModel, demoUpdateModel } from './function/model/crud.functions';
import { profileSetUsernameKey } from '@dereekb/demo-firebase';
import { NestAppPromiseGetter, nestServerInstance } from '@dereekb/firebase-server';
import { initEnvironmentForScheduleConfiguredFunctions, nestAppHasDevelopmentSchedulerEnabled, NestAppPromiseGetter, nestServerInstance } from '@dereekb/firebase-server';
import { UPDATE_MODEL_APP_FUNCTION_KEY, DELETE_MODEL_APP_FUNCTION_KEY, CREATE_MODEL_APP_FUNCTION_KEY } from '@dereekb/firebase';
import { DemoApiAppModule } from './app.module';
import { profileSetUsername, initUserOnCreate } from './function';
import { demoExampleUsageOfSchedule } from './function/model/schedule.functions';

export const { initNestServer } = nestServerInstance({
moduleClass: DemoApiAppModule,
Expand Down Expand Up @@ -32,3 +33,21 @@ export function allAppFunctions(nest: NestAppPromiseGetter) {
// Guestbook
};
}

/**
* Builder for all scheduled functions in the app.
*
* @param nest
*/
export function allScheduledAppFunctions(nest: NestAppPromiseGetter) {
return initEnvironmentForScheduleConfiguredFunctions(
{
// Scheduled Functions
exampleSchedule: demoExampleUsageOfSchedule(nest)
},
{
// Only enable when the development scheduler is enabled
checkEnabled: nestAppHasDevelopmentSchedulerEnabled(nest)
}
);
}
5 changes: 5 additions & 0 deletions apps/demo-api/src/app/function/example/example.schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DemoScheduleFunction } from '../function';

export const exampleUsageOfSchedule: DemoScheduleFunction = (request) => {
console.log('exampleUsageOfSchedule() was called!');
};
29 changes: 25 additions & 4 deletions apps/demo-api/src/app/function/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { INestApplicationContext } from '@nestjs/common';
import { DemoFirebaseContextAppContext, demoFirebaseModelServices, DemoFirebaseModelTypes, DemoFirestoreCollections } from '@dereekb/demo-firebase';
import { onCallWithNestApplicationFactory, onCallWithNestContextFactory, taskQueueFunctionHandlerWithNestContextFactory, cloudEventHandlerWithNestContextFactory, blockingFunctionHandlerWithNestContextFactory, onEventWithNestContextFactory, AbstractFirebaseNestContext, OnCallUpdateModelFunction, OnCallUpdateModelMap, OnCallDeleteModelMap, OnCallDeleteModelFunction, OnCallCreateModelFunction, OnCallCreateModelMap } from '@dereekb/firebase-server';
import {
onCallWithNestApplicationFactory,
onCallWithNestContextFactory,
taskQueueFunctionHandlerWithNestContextFactory,
cloudEventHandlerWithNestContextFactory,
blockingFunctionHandlerWithNestContextFactory,
onEventWithNestContextFactory,
AbstractFirebaseNestContext,
OnCallUpdateModelFunction,
OnCallUpdateModelMap,
OnCallDeleteModelMap,
OnCallDeleteModelFunction,
OnCallCreateModelFunction,
OnCallCreateModelMap,
onScheduleWithNestApplicationFactory,
onScheduleWithNestContextFactory,
OnScheduleWithNestContext
} from '@dereekb/firebase-server';
import { OnCallCreateModelResult } from '@dereekb/firebase';
import { ProfileServerActions, GuestbookServerActions, DemoApiAuthService, DemoFirebaseServerActionsContext } from '../common';

Expand Down Expand Up @@ -37,17 +54,21 @@ export class DemoApiNestContext extends AbstractFirebaseNestContext<DemoFirebase
export const mapDemoApiNestContext = (nest: INestApplicationContext) => new DemoApiNestContext(nest);
export const onCallWithDemoNest = onCallWithNestApplicationFactory();
export const onCallWithDemoNestContext = onCallWithNestContextFactory(onCallWithDemoNest, mapDemoApiNestContext);
export const onScheduleWithDemoNest = onScheduleWithNestApplicationFactory();
export const onScheduleWithDemoNestContext = onScheduleWithNestContextFactory(onScheduleWithDemoNest, mapDemoApiNestContext);
export const onEventWithDemoNestContext = onEventWithNestContextFactory(mapDemoApiNestContext);
export const cloudEventWithDemoNestContext = cloudEventHandlerWithNestContextFactory(mapDemoApiNestContext);
export const blockingEventWithDemoNestContext = blockingFunctionHandlerWithNestContextFactory(mapDemoApiNestContext);
export const taskqueueEventWithDemoNestContext = taskQueueFunctionHandlerWithNestContextFactory(mapDemoApiNestContext);

// MARK: CRUD Functions
export type DemoCreateModelfunction<I, O extends OnCallCreateModelResult = OnCallCreateModelResult> = OnCallCreateModelFunction<DemoApiNestContext, I, O>;
export type DemoCreateModelFunction<I, O extends OnCallCreateModelResult = OnCallCreateModelResult> = OnCallCreateModelFunction<DemoApiNestContext, I, O>;
export type DemoOnCallCreateModelMap = OnCallCreateModelMap<DemoApiNestContext, DemoFirebaseModelTypes>;

export type DemoUpdateModelfunction<I, O = void> = OnCallUpdateModelFunction<DemoApiNestContext, I, O>;
export type DemoUpdateModelFunction<I, O = void> = OnCallUpdateModelFunction<DemoApiNestContext, I, O>;
export type DemoOnCallUpdateModelMap = OnCallUpdateModelMap<DemoApiNestContext, DemoFirebaseModelTypes>;

export type DemoDeleteModelfunction<I, O = void> = OnCallDeleteModelFunction<DemoApiNestContext, I, O>;
export type DemoDeleteModelFunction<I, O = void> = OnCallDeleteModelFunction<DemoApiNestContext, I, O>;
export type DemoOnCallDeleteModelMap = OnCallDeleteModelMap<DemoApiNestContext, DemoFirebaseModelTypes>;

export type DemoScheduleFunction = OnScheduleWithNestContext<DemoApiNestContext>;
4 changes: 2 additions & 2 deletions apps/demo-api/src/app/function/guestbook/guestbook.crud.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CreateGuestbookParams } from '@dereekb/demo-firebase';
import { onCallCreateModelResultWithDocs } from '@dereekb/firebase';
import { DemoCreateModelfunction } from '../function';
import { DemoCreateModelFunction } from '../function';

export const createGuestbook: DemoCreateModelfunction<CreateGuestbookParams> = async (request) => {
export const createGuestbook: DemoCreateModelFunction<CreateGuestbookParams> = async (request) => {
const { nest, auth, data } = request;
const createGuestbook = await nest.guestbookActions.createGuestbook(data);
const guestbook = await createGuestbook();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UpdateGuestbookEntryParams } from '@dereekb/demo-firebase';
import { DemoUpdateModelfunction } from '../function';
import { DemoUpdateModelFunction } from '../function';
import { guestbookEntryForUser } from './guestbook.util';

export const updateGuestbookEntry: DemoUpdateModelfunction<UpdateGuestbookEntryParams> = async (request) => {
export const updateGuestbookEntry: DemoUpdateModelFunction<UpdateGuestbookEntryParams> = async (request) => {
const { nest, auth, data } = request;
const guestbookEntryUpdateEntry = await nest.guestbookActions.updateGuestbookEntry(data);

Expand Down
10 changes: 10 additions & 0 deletions apps/demo-api/src/app/function/model/schedule.functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { exampleUsageOfSchedule } from '../example/example.schedule';
import { onScheduleWithDemoNestContext } from '../function';

// MARK: Example
export const demoExampleUsageOfSchedule = onScheduleWithDemoNestContext(
{
cron: 1
},
exampleUsageOfSchedule
);
8 changes: 4 additions & 4 deletions apps/demo-api/src/app/function/profile/profile.update.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { FinishOnboardingProfileParams, ProfileDocument, SetProfileUsernameParams, UpdateProfileParams } from '@dereekb/demo-firebase';
import { DemoUpdateModelfunction } from '../function';
import { DemoUpdateModelFunction } from '../function';
import { profileForUserRequest } from './profile.util';
import { userHasNoProfileError } from '../../common';
import { AUTH_ONBOARDED_ROLE, AUTH_TOS_SIGNED_ROLE } from '@dereekb/util';

export const updateProfile: DemoUpdateModelfunction<UpdateProfileParams> = async (request) => {
export const updateProfile: DemoUpdateModelFunction<UpdateProfileParams> = async (request) => {
const { nest, auth, data } = request;
const updateProfile = await nest.profileActions.updateProfile(data);
const profileDocument: ProfileDocument = await profileForUserRequest(request);
await updateProfile(profileDocument);
};

export const updateProfileUsername: DemoUpdateModelfunction<SetProfileUsernameParams> = async (request) => {
export const updateProfileUsername: DemoUpdateModelFunction<SetProfileUsernameParams> = async (request) => {
const { nest, auth, data } = request;
const setProfileUsername = await nest.profileActions.setProfileUsername(data);
const profileDocument: ProfileDocument = await profileForUserRequest(request);
Expand All @@ -24,7 +24,7 @@ export const updateProfileUsername: DemoUpdateModelfunction<SetProfileUsernamePa
await setProfileUsername(profileDocument);
};

export const updateProfleOnboarding: DemoUpdateModelfunction<FinishOnboardingProfileParams, boolean> = async (request) => {
export const updateProfleOnboarding: DemoUpdateModelFunction<FinishOnboardingProfileParams, boolean> = async (request) => {
const { nest, auth, data } = request;
const uid = auth.uid;

Expand Down
8 changes: 7 additions & 1 deletion apps/demo-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { environment } from './environments/environment';
import 'reflect-metadata';
import { onRequest } from 'firebase-functions/v2/https';
import * as admin from 'firebase-admin';
import { allAppFunctions, initNestServer } from './app/app';
import { allAppFunctions, allScheduledAppFunctions, initNestServer } from './app/app';

const app = admin.initializeApp();

const { server, nest } = initNestServer(app, { environment });

export const api = onRequest(server);

// App Functions
export const { initUserOnCreate, profileSetUsername, createModel, updateModel, deleteModel } = allAppFunctions(nest);

// Scheduled Functions
const scheduledFunctions = allScheduledAppFunctions(nest);

export const { exampleSchedule } = scheduledFunctions;
Loading

0 comments on commit 2114446

Please sign in to comment.