Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-disable no-console */
import fs from 'fs';
import uuidv4 from 'uuid/v4';
import firebaseAdmin from 'firebase-admin';
import { Discussion, Pub, Collection, CollectionPub, Version } from '../v5/models';
import { nestDiscussionsToThreads } from '../../../client/containers/Pub/PubDocument/PubDiscussions/discussionUtils';
const serviceAccount = JSON.parse(
Buffer.from(process.env.V5_FIREBASE_SERVICE_ACCOUNT_BASE64, 'base64').toString(),
);
const firebaseApp =
firebaseAdmin.apps.length > 0
? firebaseAdmin.apps[0]
: firebaseAdmin.initializeApp(
{
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: process.env.V5_FIREBASE_URL,
},
'firebase-pub-new',
);
const database = firebaseApp.database();
const workingDirectory = process.env.MIGRATION_WORKING_DIRECTORY;
const frankenbookFile = fs.readFileSync(`${workingDirectory}/frankenbook-firebase.json`);
const frankenbookVersionFile = fs.readFileSync(`${workingDirectory}/frankenbook-version.json`);
const frankJson = JSON.parse(frankenbookFile);
const frankVersionJson = JSON.parse(frankenbookVersionFile);
const fs = require('fs');
const path = require('path');
const firebaseAdmin = require('firebase-admin');
// Required for side-effects
require('firebase/firestore');
let serviceAccountKey = false;
const serviceAccountPath = path.resolve(
__dirname,
'../../element-did-firebase-adminsdk.json',
);
if (fs.existsSync(serviceAccountPath)) {
serviceAccountKey = JSON.parse(fs.readFileSync(serviceAccountPath));
}
if (!firebaseAdmin.apps.length) {
firebaseAdmin.initializeApp({
credential: serviceAccountKey
? firebaseAdmin.credential.cert(serviceAccountKey)
: firebaseAdmin.credential.applicationDefault(),
databaseURL: 'https://element-did.firebaseio.com',
});
}
const firestore = firebaseAdmin.firestore();
module.exports = {
firestore,
};
export const sendMobileNotification = async ({
receivers,
title,
body,
customerId,
}: {
receivers: string[];
customerId?: string;
title: string;
body: string;
}): Promise => {
if (!admin.apps.length) {
return;
}
const transporter = admin.messaging();
const tokens: string[] = [];
if (receivers) {
tokens.push(...(await Users.find({ _id: { $in: receivers } }).distinct('deviceTokens')));
}
if (customerId) {
tokens.push(...(await Customers.findOne({ _id: customerId }).distinct('deviceTokens')));
}
if (tokens.length > 0) {
// send notification
constructor(option: FirestoreOption) {
this.db = isEmpty(admin.apps)
? admin.initializeApp(option.config).firestore()
: admin.app().firestore();
this.collection = option.collection;
// path exists
if (option.path && option.path.indexOf('/') > 0) {
this.path = option.path;
} else if (option.path) {
this.path = option.path.startsWith('/')
? `${defaultMapCollection}${option.path}`
: `${defaultMapCollection}/${option.path}`;
}
}
export const sendMobileNotification = async ({
receivers,
title,
body,
customerId,
conversationId,
}: {
receivers: string[];
customerId?: string;
title: string;
body: string;
conversationId: string;
}): Promise => {
if (!admin.apps.length) {
return;
}
const transporter = admin.messaging();
const tokens: string[] = [];
if (receivers) {
tokens.push(...(await Users.find({ _id: { $in: receivers } }).distinct('deviceTokens')));
}
if (customerId) {
tokens.push(...(await Customers.findOne({ _id: customerId }).distinct('deviceTokens')));
}
if (tokens.length > 0) {
// send notification
afterEach(() => {
_.forEach(firebase.apps, (app) => {
app.delete();
});
});
const killFirebaseApps = function() {
admin.apps.forEach(app => {
app.delete();
});
};
const admin = require("firebase-admin");
const serviceAccount = require("./firebase.json");
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://frugalmaps.firebaseio.com"
});
}
const db = admin.firestore();
module.exports = {
db
};
const _getAppIfAlreadyExists = url => {
let apps = admin.apps;
for (let i = 0; i < apps.length; i++) {
if (apps[i].name === url) {
return apps[i];
}
}
return null;
};
exports.sourceNodes = async (
{ boundActionCreators },
{ types, credential, appConfig }
) => {
try {
if (firebase.apps || !firebase.apps.length) {
const cfg = appConfig ? appConfig : {credential: firebase.credential.cert(credential)}
firebase.initializeApp(cfg);
}
} catch (e) {
report.warn(
'Could not initialize Firebase. Please check `credential` property in gatsby-config.js'
);
report.warn(e);
return;
}
const db = firebase.firestore();
db.settings({
timestampsInSnapshots: true,
});
const { createNode } = boundActionCreators;