Skip to content

Commit

Permalink
Merge pull request #45 from quinnturner/cloud-fns-cleanup
Browse files Browse the repository at this point in the history
Cloud functions updates and clean up
  • Loading branch information
quinnturner committed Sep 19, 2020
2 parents afdc1bb + 5258aac commit 9798a74
Show file tree
Hide file tree
Showing 10 changed files with 618 additions and 433 deletions.
3 changes: 2 additions & 1 deletion cloud-functions/functions/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Firestore } from '@google-cloud/firestore';
import mongoose, { ConnectionOptions } from 'mongoose';

import { Firestore } from '@google-cloud/firestore';

const firestore = new Firestore();

const dbConnectionString = process.env.MONGO_DB;
Expand Down
8 changes: 5 additions & 3 deletions cloud-functions/functions/src/discordMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Message, MessageOptions, Channel, TextChannel } from 'discord.js';
import { Firestore } from '@caravanapp/types';
import { shouldSendWithLease, markSent } from './idempotent';
import { Channel, Message, MessageOptions, TextChannel } from 'discord.js';

import type { Firestore } from '@caravanapp/types';

import { firestore } from './db';
import { markSent, shouldSendWithLease } from './idempotent';

export const sendDiscordMessage = async (
eventId: string,
Expand Down
2 changes: 1 addition & 1 deletion cloud-functions/functions/src/email.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { shouldSendWithLease, markSent } from './idempotent';
import { firestore } from './db';
import { markSent, shouldSendWithLease } from './idempotent';

export const sendEmail = async (eventId: string) => {
const emailRef = firestore.collection('sentEmails').doc(eventId);
Expand Down
5 changes: 3 additions & 2 deletions cloud-functions/functions/src/idempotent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DocumentReference, DocumentData } from '@google-cloud/firestore';
import type { DocumentData, DocumentReference } from '@google-cloud/firestore';

import { IdempotentSendData } from '../';
import { firestore } from './db';
import { IdempotentSendData } from '..';

const leaseTime = 60 * 1000; // 60s

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CloudFunctionsContext } from '@google-cloud/functions-framework';
import type { CloudFunctionsContext } from '@google-cloud/functions-framework/build/src/functions';
import type { PubSub as PubSubTypes } from '@caravanapp/types';
import type { EventData } from '../..';
import { connect } from '../db';
import { ReadingDiscordBot } from '../discord';
import { sendDiscordMessage } from '../discordMessenger';
import { getUser, getUserProfileUrl } from '../services/user';
import { getClub } from '../services/club';
import { connect } from '../db';
import { EventData } from '../..';
import { getUser, getUserProfileUrl } from '../services/user';

export const onJoinClub = async (
context: Required<CloudFunctionsContext>,
Expand Down
5 changes: 3 additions & 2 deletions cloud-functions/functions/src/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { GuildMember } from 'discord.js';
import type { GuildMember } from 'discord.js';

import { checkObjectIdIsValid } from '../workspace/mongo/common/mongoose';
// These imports will only exist after building
import { UserDoc, UserModel } from '../workspace/mongo/models/user';
import { checkObjectIdIsValid } from '../workspace/mongo/common/mongoose';

export const getUserProfileUrl = (urlSlug: string) =>
`https://caravanapp.ca/user/${urlSlug}`;
Expand Down
3 changes: 2 additions & 1 deletion cloud-functions/functions/src/test/sendgrid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HttpFunction } from '@google-cloud/functions-framework/build/src/functions';
import sgMail from '@sendgrid/mail';
import { HttpFunction } from '@google-cloud/functions-framework';

import { loadLocalEnv } from './loadenv';

export const testSendGrid: HttpFunction = async (req, res) => {
Expand Down
1,013 changes: 596 additions & 417 deletions cloud-functions/functions/yarn.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/mongo/src/common/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema, SchemaType, SchemaTypeOpts, Types } from 'mongoose';

import type { FilterAutoMongoKeys } from '@caravanapp/types';

export function checkObjectIdIsValid(id: string) {
export function checkObjectIdIsValid(id: string): boolean {
return (
Types.ObjectId.isValid(id) && new Types.ObjectId(id).toHexString() === id
);
Expand Down
2 changes: 1 addition & 1 deletion packages/mongo/src/models/club.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const shelfSchemaDefinition: MongooseSchema<ShelfEntry> = {
required: true,
index: true,
validate: {
validator: function (v: BookSource) {
validator: function (v: BookSource): boolean {
return ALLOWED_BOOK_SOURCES[v] === true;
},
},
Expand Down

0 comments on commit 9798a74

Please sign in to comment.