Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
feat(cronChapters): save directory into storage bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jul 16, 2019
1 parent 0173319 commit c9722b8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ testem.log
# System Files
.DS_Store
Thumbs.db

/.firebase
firebase-debug.log
8 changes: 8 additions & 0 deletions cors-json-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"origin": ["*"],
"responseHeader": ["Content-Type"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
6 changes: 5 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
55 changes: 32 additions & 23 deletions functions/src/cron/chapters.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<any>[] = [];
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<any>[] = [];
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)));
});
4 changes: 3 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as admin from 'firebase-admin';

admin.initializeApp();
admin.initializeApp({
storageBucket: 'withgdg.appspot.com'
});

export * from './cron/chapters';
14 changes: 13 additions & 1 deletion functions/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 8 additions & 0 deletions storage.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if true;
allow write: if false;
}
}
}

0 comments on commit c9722b8

Please sign in to comment.