Skip to content

Commit

Permalink
[Easy Dev] Add a script to upload a partner from partners.json to fir…
Browse files Browse the repository at this point in the history
…estore.
  • Loading branch information
zozoens31 committed Jan 8, 2021
1 parent 5ecf841 commit 1ebc59c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ services:
- ./server/tsconfig.json:/usr/app/tsconfig.json
- ./server/public:/usr/app/public
- ./server/src:/usr/app/src
- ./server/upload_partner.ts:/usr/app/upload_partner.ts
- ./credentials:/usr/app/credentials:ro
- ./src/store/partners.json:/usr/app/partners.json
server-test:
volumes:
- ./server/package.json:/usr/app/package.json
Expand Down
2 changes: 1 addition & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FROM base AS test
RUN apt-get update -qqy && apt-get install -qqy jq openjdk-8-jdk-headless && \
node_modules/.bin/firebase emulators:exec --only firestore 'echo "Emulator preloaded"'

COPY .eslintrc.json .eslintignore server/.firebaserc server/clean_dependencies.sh server/entrypoint.sh server/firebase.json server/firestore.indexes.json server/firestore.rules server/tsconfig.json ./
COPY .eslintrc.json .eslintignore server/.firebaserc server/clean_dependencies.sh server/entrypoint.sh server/firebase.json server/firestore.indexes.json server/firestore.rules server/tsconfig.json server/upload_partner.ts src/store/partners.json ./
COPY server/src src/
COPY server/test test/

Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"deploy": "firebase deploy",
"logs": "firebase functions:log",
"predeploy": "npm run apidoc",
"test": "firebase emulators:exec --only firestore 'mocha test/*.ts --require ts-node/register'"
"test": "firebase emulators:exec --only firestore 'mocha test/*.ts --require ts-node/register'",
"upload": "GOOGLE_APPLICATION_CREDENTIALS='./credentials/serviceAccountKey.json' ts-node upload_partner.ts"
},
"main": "lib/index.js",
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es2019"],
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src"
"src",
"upload_parters.ts"
]
}
48 changes: 48 additions & 0 deletions server/upload_partner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* A script to upload a partner info to firestore.
*
* First, get a service account key in https://console.firebase.google.com/u/0/project/ma-voie/settings/serviceaccounts/adminsdk
* and save it as credentials/serviceAccountKey.json
*
* Then run `npm run upload -- partner-id`
*
* This will upload the partner with the given ID as a document in firestore in production.
* The newly created document has its partnerId as ID.
*
* To upload to demo or dev, simply use a relevant serviceAccountKey.json.
*/
/* eslint-disable no-console */
import admin from 'firebase-admin'

import partners from './partners.json'

const partnerId = process.argv[2]
console.log(`Starting to upload ${partnerId}...`)
if (!partnerId) {
console.error('You must specify a partner to upload')
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1)
}

admin.initializeApp({
credential: admin.credential.applicationDefault(),
})

console.log('App initialized');

(async (): Promise<void> => {
const partner = partners.find(({partnerId: pId}: {partnerId: string}) => pId === partnerId)
if (!partner) {
console.error(
`Could not find a partner with ID ${partnerId}. Choose one from`,
partners.map(({partnerId}) => partnerId),
)
return
}
const db = admin.firestore()
try {
await db.collection('staticPartners').doc(partnerId).set(partner)
console.log(`Partner ${partnerId} has been uploaded to firestore.`)
} catch (error) {
console.error(`An error occurred while uploading partner ${partnerId}`, error)
}
})()

0 comments on commit 1ebc59c

Please sign in to comment.