diff --git a/.gitignore b/.gitignore index f4f46a5..15dcbcf 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ testem.log # System Files .DS_Store Thumbs.db + +/.firebase +firebase-debug.log diff --git a/cors-json-file.json b/cors-json-file.json new file mode 100644 index 0000000..cde6390 --- /dev/null +++ b/cors-json-file.json @@ -0,0 +1,8 @@ +[ + { + "origin": ["*"], + "responseHeader": ["Content-Type"], + "method": ["GET"], + "maxAgeSeconds": 3600 + } +] diff --git a/firebase.json b/firebase.json index 178c518..be219fe 100644 --- a/firebase.json +++ b/firebase.json @@ -22,6 +22,10 @@ "indexes": "firestore.indexes.json" }, "functions": { - "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build" + "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build", + "source": "functions" + }, + "storage": { + "rules": "storage.rules" } } diff --git a/functions/src/cron/chapters.ts b/functions/src/cron/chapters.ts index d08e5e8..692cb55 100644 --- a/functions/src/cron/chapters.ts +++ b/functions/src/cron/chapters.ts @@ -1,6 +1,9 @@ import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; +import { writeFileSync } from 'fs'; import { GeoFirestore } from 'geofirestore'; +import { join } from 'path'; +import { tmpdir } from 'os'; import request from '../shared/request'; import WoomeraTypes from '../types'; @@ -13,32 +16,38 @@ export const cronChapters = functions.pubsub.schedule('0 0 1 * *').onRun(() => { }).then((result: WoomeraTypes.ChaptersRequest) => { const geofirestore = new GeoFirestore(admin.firestore()); const collection = geofirestore.collection('chapters'); - const chapterBatches: WoomeraTypes.Chapter[][] = []; - const batches: Promise[] = []; const updatedOn = new Date().getTime(); + const fileName = 'directory.json'; + const tempFilePath = join(tmpdir(), fileName); - while (result.data.length) { - const temp = result.data.splice(0, 500).map((chapter) => ({ - city: chapter.cityarea, - country: chapter.country, - region: chapter.region, - coordinates: new admin.firestore.GeoPoint(chapter.geo.lat, chapter.geo.lng), - name: chapter.chapter_name, - updatedOn, - $key: chapter.website.replace(/(http|https):\/\/(.+.)?meetup.com\//, '').replace('/', '').toLowerCase() - })); - chapterBatches.push(temp); - } + const chapters: WoomeraTypes.ChapterSimple[] = result.data.map((chapter) => ({ + city: chapter.cityarea, + country: chapter.country, + region: chapter.region, + coordinates: { lat: chapter.geo.lat, lng: chapter.geo.lng }, + name: chapter.chapter_name, + updatedOn, + $key: chapter.website.replace(/(http|https):\/\/(.+.)?meetup.com\//, '').replace('/', '').toLowerCase() + })); + writeFileSync(tempFilePath, JSON.stringify(chapters)); - chapterBatches.forEach((chapters) => { - const batch = geofirestore.batch(); - chapters.forEach((chapter) => { - const insert = collection.doc(chapter.$key); - batch.set(insert, chapter); + return admin.storage().bucket() + .upload(tempFilePath, { destination: fileName }) + .then(() => { + const batches: Promise[] = []; + while (chapters.length) { + const batch = geofirestore.batch(); + chapters.splice(0, 500).map((data) => { + const chapter: WoomeraTypes.Chapter = { + ...data, + coordinates: new admin.firestore.GeoPoint(data.coordinates.lat, data.coordinates.lng) + }; + const insert = collection.doc(chapter.$key); + batch.set(insert, chapter); + }); + batches.push(batch.commit()); + } + return Promise.all(batches); }); - batches.push(batch.commit()); - }); - - return Promise.all(batches); }).catch((error: any) => console.log('ERROR: ' + JSON.stringify(error))); }); diff --git a/functions/src/index.ts b/functions/src/index.ts index e79d34c..be28556 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,5 +1,7 @@ import * as admin from 'firebase-admin'; -admin.initializeApp(); +admin.initializeApp({ + storageBucket: 'withgdg.appspot.com' +}); export * from './cron/chapters'; diff --git a/functions/src/types/index.ts b/functions/src/types/index.ts index af6bd78..85edbab 100644 --- a/functions/src/types/index.ts +++ b/functions/src/types/index.ts @@ -7,7 +7,19 @@ export namespace WoomeraTypes { region: string; coordinates: admin.firestore.GeoPoint; name: string; - updatedOn: number; + updatedOn?: number; + $key: string; + }; + export type ChapterSimple = { + city: string; + country: string; + region: string; + coordinates: { + lat: number; + lng: number; + }; + name: string; + updatedOn?: number; $key: string; }; export type ChapterRequestData = { diff --git a/storage.rules b/storage.rules new file mode 100644 index 0000000..93152d9 --- /dev/null +++ b/storage.rules @@ -0,0 +1,8 @@ +service firebase.storage { + match /b/{bucket}/o { + match /{allPaths=**} { + allow read: if true; + allow write: if false; + } + } +}