Skip to content

Commit

Permalink
Pre-requisite for setup datasources (#83)
Browse files Browse the repository at this point in the history
* Remove unused files

* delete deepsource integration

* Add [src] to watch for restart on PM2

* Parse JWT for createdBy and UpdatedBy

* use updated cBy and uBY for sign mutation

* Use Transactions for saving sign for users

* Setup Flat Types for nested types

* Set sanitizeFilter: true

* fetch user by JWT
  • Loading branch information
DesignrKnight authored Nov 15, 2021
1 parent 0ffb30d commit 0d70f27
Show file tree
Hide file tree
Showing 26 changed files with 183 additions and 522 deletions.
13 changes: 0 additions & 13 deletions .deepsource.toml

This file was deleted.

2 changes: 1 addition & 1 deletion ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
{
name: 'dev',
script: './src/index.js',
watch: '.',
watch: ['src'],
env: {
NODE_ENV: 'development',
},
Expand Down
47 changes: 27 additions & 20 deletions src/graphql/mutation/sign.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
const { GraphQLString, GraphQLNonNull, GraphQLID } = require('graphql');
const { GraphQLString, GraphQLNonNull } = require('graphql');
const mongoose = require('mongoose');

// Type Defs
const SignType = require('../types/sign');
const { SignType } = require('../types/');

const UserModel = require('../../models/user');
const SignModel = require('../../models/sign');

const { addCreatedAndUpdatedBy } = require('../../utils/index');
const { GraphQLError } = require('graphql');

const createSign = {
name: 'createSign',
type: SignType,
args: {
userID: { type: GraphQLID },
userMail: { type: GraphQLString },
name: { type: GraphQLNonNull(GraphQLString) },
image: { type: GraphQLNonNull(GraphQLString) },
designation: { type: GraphQLNonNull(GraphQLString) },
},
async resolve(_, { userID, userMail, name, image, designation }) {
if (userID) {
const ifUserExists = await UserModel.exists({ _id: userID }).exec();
if (ifUserExists) {
const sign = new SignModel({ userID, userMail, name, image, designation, ...addCreatedAndUpdatedBy(null) });
return sign.save();
}
return new GraphQLError('User Not in Database');
async resolve(_, { name, image, designation }, { decodedToken, addCreatedAndUpdatedByWithUser }) {
if (!decodedToken || !decodedToken.sub) {
return new GraphQLError('Missing fields in the Auth Token');
}
if (userMail) {
const userID = await UserModel.findOne({ mail: userMail }, '_id').exec();
if (userID) {
const sign = new SignModel({ userID, userMail, name, image, designation, ...addCreatedAndUpdatedBy(null) });
return sign.save();
}
const { sub } = decodedToken;
const userFromDB = await UserModel.findOne({ authProviderID: sub }).setOptions({ sanitizeFilter: true }).exec();
if (!userFromDB._id) {
return new GraphQLError('User Not in Database');
}
return new GraphQLError('Missing fields');
const session = await mongoose.startSession();
let response;
await session.withTransaction(async () => {
const signToSave = new SignModel({
userID: userFromDB._id,
name,
image,
designation,
...addCreatedAndUpdatedByWithUser(),
});
const signWithID = await signToSave.save();
userFromDB.signs.push(signWithID._id);
await userFromDB.save();
response = signWithID;
});
session.endSession();
return response;
},
};

Expand Down
11 changes: 5 additions & 6 deletions src/graphql/mutation/user.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
const { GraphQLString, GraphQLNonNull, GraphQLError } = require('graphql');

// Type Defs
const UserType = require('../types/user');
const { UserType } = require('../types');

const User = require('../../models/user');

const { addCreatedAndUpdatedBy } = require('../../utils/index');

const createUser = {
name: 'createUser',
type: UserType,
args: {
name: { type: GraphQLNonNull(GraphQLString) },
displayPicture: { type: GraphQLNonNull(GraphQLString) },
},
resolve(_, { name, displayPicture }, { decodedToken }) {
if (!decodedToken) {
resolve(_, { name, displayPicture }, { decodedToken, addCreatedAndUpdatedByWithUser }) {
if (!decodedToken || !decodedToken.email || !decodedToken.sub) {
return new GraphQLError('Missing fields in the Auth Token');
}
const { email: mail, sub: authProviderID } = decodedToken;
Expand All @@ -23,7 +22,7 @@ const createUser = {
name,
displayPicture,
authProviderID,
...addCreatedAndUpdatedBy(null),
...addCreatedAndUpdatedByWithUser(),
});
return user.save();
},
Expand Down
54 changes: 0 additions & 54 deletions src/graphql/mutation/welcome.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/graphql/query/Welcome.js

This file was deleted.

24 changes: 7 additions & 17 deletions src/graphql/query/sign.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
const { GraphQLError, GraphQLID, GraphQLList } = require('graphql');
const { GraphQLID, GraphQLNonNull } = require('graphql');
// Type Defs
const SignType = require('../types/sign');
const { SignType } = require('../types/');

// Models
const SignModel = require('../../models/sign');
const UserModel = require('../../models/user');
const getSign = {
type: GraphQLList(SignType),
name: 'getSign',
type: SignType,
args: {
signId: { type: GraphQLID },
userId: { type: GraphQLID },
id: { type: GraphQLNonNull(GraphQLID) },
},
async resolve(_, { signId, userId }) {
if (signId) {
return [SignModel.findById(signId).exec()];
}
if (userId) {
return (
(await UserModel.findById(userId).select('signs').populate({ path: 'signs', model: 'sign' }).exec())?.signs ||
[]
);
}
return new GraphQLError('signId or userId is required');
resolve(_, { id }) {
return SignModel.findById(id).exec();
},
};

Expand Down
11 changes: 8 additions & 3 deletions src/graphql/query/user.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
const { GraphQLError, GraphQLID, GraphQLString } = require('graphql');
// Type Defs
const UserType = require('../types/user');
const { UserType } = require('../types/');

// Models
const UserModel = require('../../models/user');

const getUser = {
name: 'getUser',
type: UserType,
args: {
id: { type: GraphQLID },
mail: { type: GraphQLString },
},
resolve(_, { id, mail }) {
resolve(_, { id, mail }, { decodedToken }) {
const { sub } = decodedToken || {};
if (sub) {
return UserModel.findOne({ authProviderID: sub }).setOptions({ sanitizeFilter: true }).exec();
}
if (id) {
return UserModel.findById(id).exec();
}
if (mail) {
return UserModel.findOne({ mail }).exec();
return UserModel.findOne({ mail }).setOptions({ sanitizeFilter: true }).exec();
}
return new GraphQLError('Missing fields');
},
Expand Down
30 changes: 0 additions & 30 deletions src/graphql/resolver/addUser.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/graphql/resolver/index.js

This file was deleted.

26 changes: 0 additions & 26 deletions src/graphql/resolver/updateUser.js

This file was deleted.

60 changes: 0 additions & 60 deletions src/graphql/types/CertificateType.js

This file was deleted.

Loading

0 comments on commit 0d70f27

Please sign in to comment.