Skip to content

Instantly share code, notes, and snippets.

View richartkeil's full-sized avatar

Richard Keil richartkeil

View GitHub Profile
interface Event {
public isCritical(): boolean;
public getName(): string;
public getPayload(): object;
}
class UserProcessed implements Event {
constructor(protected user: firestore.DocumentSnapshot) {}
public isCritical() {
class Account {
constructor(protected accountId: string) {}
public async addEvent(event: Event) {
return firestore()
.collection("accounts")
.doc(this.accountId)
.collection("events")
.add({
name: event.getName(),
const processUser = functions.firestore
.document("accounts/{accountId}/users/{userId}")
.onCreate(async (snapshot, context) => {
try {
await processTheUser(snapshot);
const event = new UserProcessed(snapshot);
await new Account(context.params.accountId).addEvent(event);
} catch (error) {
const event = new UserProcessingFailed(snapshot, error);
await new Account(context.params.accountId).addEvent(event);