Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: google docs using google api
Browse files Browse the repository at this point in the history
120EE0692 committed Mar 11, 2024
1 parent 431da79 commit fb436e0
Showing 9 changed files with 276 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -562,6 +562,7 @@ out

# Roles Cache
roles.json
firebaseServiceAccount.json

# Environment Variables
.env.*
12 changes: 9 additions & 3 deletions server/config/firebase.js
Original file line number Diff line number Diff line change
@@ -11,19 +11,25 @@

const Admin = require('firebase-admin');
const logger = require('../utils/logger')('firebase');
const fs = require('fs');

module.exports = {
/**
* @description Firebase Initialization Sequence
* @function
*/
init: () => {
const firebaseServiceAccount = JSON.parse(
Buffer.from(process.env.FIREBASE_SERVICE_ACCOUNT, 'base64').toString('ascii')
);

if (!fs.existsSync('firebaseServiceAccount.json')) {
fs.writeFileSync('firebaseServiceAccount.json', JSON.stringify(firebaseServiceAccount));
}

try {
/** Inititalize Firebase Admin SDK with required configuration */
if (process.env.FIREBASE_SERVICE_ACCOUNT && process.env.NODE_ENV !== 'development') {
const firebaseServiceAccount = JSON.parse(
Buffer.from(process.env.FIREBASE_SERVICE_ACCOUNT, 'base64').toString('ascii')
);
Admin.initializeApp({
credential: Admin.credential.cert(firebaseServiceAccount),
storageBucket: process.env.FIREBASE_STORAGE_BUCKET || null,
2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -43,6 +43,8 @@
]
},
"dependencies": {
"@googleapis/docs": "^3.0.0",
"@googleapis/drive": "^8.6.0",
"@graphql-tools/stitch": "^8.3.1",
"apollo-server-core": "^3.10.1",
"apollo-server-express": "^3.4.0",
60 changes: 57 additions & 3 deletions server/schema/article/article.datasources.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,54 @@ const UserModel = require('../user/user.model');
const { connection } = require('../../config/mongoose');
const createUpdateObject = require('../../utils/createUpdateObject');
const TagModel = require('../tag/tag.model');
const docs = require('@googleapis/docs');
const drive = require('@googleapis/drive');

let auth;

const googleAuth = () => {
auth = new drive.auth.GoogleAuth({
keyFilename: './firebaseServiceAccount.json',
scopes: ['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive'],
});
};

const createGoogleDoc = async (title = 'test', emails) => {
try {
googleAuth();
const authClient = await auth.getClient();

const driveClient = drive.drive({
version: 'v3',
auth: authClient,
});

const driveResponse = await driveClient.files.create({
requestBody: {
name: title,
mimeType: 'application/vnd.google-apps.document',
},
});

const googleDocsId = driveResponse.data.id;

await emails.forEach(async (email) => {
driveClient.permissions.create({
fileId: googleDocsId,
requestBody: {
role: 'writer',
type: 'user',
transferOwnership: true,
emailAddress: email,
},
});
});

return googleDocsId;
} catch (error) {
console.log('error:', error);
}
};

const ARTICLE_PUBLISH_TYPES = Object.fromEntries(
PublishStatusEnumType.getValues().map((item) => [item.name, item.value])
@@ -196,6 +244,7 @@ const create = async (
photographers,
designers,
tech,
emails,
categories,
session,
authToken,
@@ -214,13 +263,18 @@ const create = async (
'At least one of the user IDs supplied supplied is invalid, i.e. that user does not exist or is not a MM Team Member.',
});
}

// TODO: create google doc and link
let googleDocsId;
try {
googleDocsId = await createGoogleDoc(title, emails);
} catch (error) {
throw APIError(`Google docs can not be created`, null, { reason: error });
}
const [_article] = await ArticleModel.create(
[
{
articleType,
title,
googleDocsId,
users: _users.map((_user) => ({
name: _user.fullName,
team: authors.includes(_user._id.toString())
@@ -235,7 +289,7 @@ const create = async (
details: _user._id,
})),
categories,
createdBy: UserSession.valid(session, authToken) ? mid : null,
createdBy: UserSession.valid(session, authToken) ? mid || null : null,
},
],
{ session: mdbSession }
4 changes: 4 additions & 0 deletions server/schema/article/article.model.js
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ const ArticleSchema = new Schema(
min: 0,
max: 2,
},
googleDocsId: {
type: String,
required: false,
},
title: {
type: String,
required: true,
1 change: 1 addition & 0 deletions server/schema/article/article.mutation.js
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ module.exports = new GraphQLObjectType({
photographers: { type: new GraphQLNonNull(new GraphQLList(GraphQLID)) },
designers: { type: new GraphQLNonNull(new GraphQLList(GraphQLID)) },
tech: { type: new GraphQLNonNull(new GraphQLList(GraphQLID)) },
emails: { type: new GraphQLNonNull(new GraphQLList(GraphQLString)) },
categoryNumbers: { type: new GraphQLNonNull(new GraphQLList(GraphQLInt)) },
},
resolve: createArticle,
3 changes: 2 additions & 1 deletion server/schema/article/article.resolver.js
Original file line number Diff line number Diff line change
@@ -347,7 +347,7 @@ module.exports = {

createArticle: async (
_parent,
{ articleType, title, authors, photographers, designers, tech, categoryNumbers },
{ articleType, title, authors, photographers, designers, tech, emails, categoryNumbers },
{ mid, session, authToken, decodedToken, API: { Article, CategoryMap } }
) => {
try {
@@ -377,6 +377,7 @@ module.exports = {
photographers,
designers,
tech,
emails,
categories,
session,
authToken,
2 changes: 1 addition & 1 deletion server/schema/article/article.type.js
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ const ArticleType = new GraphQLObjectType({
id: { type: GraphQLID },

articleType: { type: ArticleTypeEnumType },

googleDocsId: { type: GraphQLString },
title: { type: GraphQLString },
content: { type: new GraphQLList(ContentType) },
inshort: { type: GraphQLString },
199 changes: 199 additions & 0 deletions server/yarn.lock

Large diffs are not rendered by default.

0 comments on commit fb436e0

Please sign in to comment.