diff --git a/firebase.json b/firebase.json index acb4a4f..88da25d 100644 --- a/firebase.json +++ b/firebase.json @@ -15,7 +15,11 @@ ], "rewrites": [ { - "source": "/api/v1/**", + "source": "/{,**}", + "destination": "/api/v1" + }, + { + "source": "/api/v1**", "function": "www-webApi" } ] diff --git a/functions/package.json b/functions/package.json index da07624..4499a69 100644 --- a/functions/package.json +++ b/functions/package.json @@ -13,7 +13,7 @@ "daemon": "cross-env-shell RUN_DAEMON=true \"npm run build && firebase emulators:start --only functions\"" }, "engines": { - "node": "10" + "node": "8" }, "main": "lib/bin/index.js", "dependencies": { diff --git a/functions/src/bin/daemon.ts b/functions/src/bin/daemon.ts index 2312f97..fad031d 100644 --- a/functions/src/bin/daemon.ts +++ b/functions/src/bin/daemon.ts @@ -3,11 +3,17 @@ import functions = require('firebase-functions'); updater.initialize() - .then(_ => updater.scheduleUpdates()); + .then(_ => updater.scheduleUpdates() + .catch(err => console.warn(`Error while scheduling updates - ${err}`))) + .catch(err => { + console.error(`Error while initializing the updater - ${err}`); + process.exit(1); + }); process.on('SIGINT', () => { updater.stopScheduling() - .then(process.exit(0)); + .finally(process.exit(0)) + .catch(err => console.warn(`Error while finishing the schedules - ${err}`)); }); exports.updater = functions.https.onRequest((req, resp) => resp.sendStatus(200)); diff --git a/functions/src/models/updater.ts b/functions/src/models/updater.ts index 979dcfb..7c756d3 100644 --- a/functions/src/models/updater.ts +++ b/functions/src/models/updater.ts @@ -20,9 +20,7 @@ export async function initialize() { initCalled = true; const projectProperties = properties.projectProperties(firebaseApp); for (const language of properties.languages) { - console.debug(`Creating updater for language ${language}`); const terms = await remoteConfig.getSearchTermsForLanguage(language); - console.debug(`Updater terms: ${terms}`); updaters[language] = new Updater( projectProperties.database, `${projectProperties.collection}_${language}`, @@ -42,7 +40,7 @@ export async function initialize() { export async function scheduleUpdates() { if (!initCalled) throw new Error('`initialize` not called'); - console.info('Updater is scheduling updates') + console.info('Updaters are scheduling updates') for (const language of properties.languages) { timers.add(updaters[language].schedule()); } diff --git a/functions/src/rcdata.ts b/functions/src/rcdata.ts index 94f6f6c..86282ec 100644 --- a/functions/src/rcdata.ts +++ b/functions/src/rcdata.ts @@ -36,6 +36,7 @@ export class RemoteConfigData { listenToRCChanges() { functions.remoteConfig.onUpdate(_ => { return admin.credential.applicationDefault().getAccessToken() + // tslint:disable-next-line:no-shadowed-variable .then(_ => { this.remoteConfig.getTemplate() .then(template => { @@ -51,7 +52,8 @@ export class RemoteConfigData { console.warn(`Updaters are not set yet - ${e}`); } } - }); + }) + .catch(err => console.warn(`Error while obtaining the template - ${err}`)); }) .catch(err => console.error(`Error while obtaining data from RC: ${err}`)); }); diff --git a/functions/src/updater.ts b/functions/src/updater.ts index 344f031..8710c91 100644 --- a/functions/src/updater.ts +++ b/functions/src/updater.ts @@ -63,16 +63,17 @@ export class Updater { async updateData(content: Array) { try { - content.forEach(element => { - firebaseHelper.firestore.checkDocumentExists(this.db, this.collectionName, element.id) - .then(exists => { - if (!exists) - firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element); - else - firebaseHelper.firestore.updateDocument(this.db, this.collectionName, element.id, element); - }) - console.log(`Created element with ID: ${element.id}`); - }); + for (const element of content) { + try { + const exists = await firebaseHelper.firestore.checkDocumentExists(this.db, this.collectionName, element.id); + if (!exists) + await firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element); + else + await firebaseHelper.firestore.updateDocument(this.db, this.collectionName, element.id, element); + } catch (err) { + console.warn(`Error while creating/updating document - ${err}`); + } + } } catch (error) { console.error(`Unhandled error ${error}`); } diff --git a/functions/tsconfig.json b/functions/tsconfig.json index 872b7f9..8ef9748 100644 --- a/functions/tsconfig.json +++ b/functions/tsconfig.json @@ -6,7 +6,8 @@ "outDir": "lib", "sourceMap": true, "strict": false, - "target": "es2017" + "target": "es2017", + "lib": ["es2015", "es2016", "dom", "es2018.promise"] }, "compileOnSave": true, "include": [