Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Easy Dev] Add a script to upload a partner from partners.json to firestore. #594

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion server/clean_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
npm run -s build
# typescript transpiles to the lib folder.
readonly REQUIRED_LIBS=$(grep -hor 'require.*' lib/ |
sed -e 's/require("//' -e 's/");//' |
sed -e 's/require("//' -e 's/"))\?;//' |
grep -v '^\.' |
sort | uniq)
readonly DEPENDENCIES=$(jq -r '.dependencies|keys|.[]' package.json)
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: 2 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
*/
import * as bodyParser from 'body-parser'
import * as functions from 'firebase-functions'
import * as express from 'express'
import {NextFunction, Request, Response} from 'express'
import * as BasicAuth from 'express-basic-auth'
import express, {NextFunction, Request, Response} from 'express'
import BasicAuth from 'express-basic-auth'

import {registerUser, validateUser} from './firestore'

Expand Down
4 changes: 2 additions & 2 deletions server/test/test_rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import * as firebase from '@firebase/testing'

chai.use(chaiAsPromised)
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_partner.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)
}
})()