Skip to content

Commit

Permalink
feat: basic integration of live page (#170)
Browse files Browse the repository at this point in the history
* fix: company schema for basic data reads

* fix: live schema for basic data reads

* fix: typo in LiveDatasources

* feat: new SemesterEnum
  • Loading branch information
rutajdash authored Dec 28, 2022
1 parent 4dc17d0 commit 07544fb
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 373 deletions.
4 changes: 2 additions & 2 deletions server/config/apolloServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const IssueDataSources = require('../schema/issue/issue.datasources');
const CategoryMapDataSources = require('../schema/categoryMap/categoryMap.datasources');
const ArticleDataSources = require('../schema/article/article.datasources');
const CompanyDataSources = require('../schema/company/company.datasources');
const LiveDataSource = require('../schema/live/live.datasources');
const LiveDataSources = require('../schema/live/live.datasources');

const APOLLO_ENDPOINT = process.env.APOLLO_ENDPOINT?.includes('herokuapp')
? process.env.APOLLO_ENDPOINT.replace('num', process.env.HEROKU_PR_NUMBER)
Expand Down Expand Up @@ -58,7 +58,7 @@ const apolloServer = (httpServer) =>
CategoryMap: CategoryMapDataSources(),
Article: ArticleDataSources(),
Company: CompanyDataSources(),
Live: LiveDataSource(),
Live: LiveDataSources(),
},
}),
debug: !process.env.NODE_ENV || process.env.NODE_ENV === 'development',
Expand Down
11 changes: 11 additions & 0 deletions server/schema/common/session.enum.type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { GraphQLEnumType } = require('../scalars');

const SemesterEnum = new GraphQLEnumType({
name: 'SemesterEnum',
values: {
AUTUMN: { value: 0 },
SPRING: { value: 1 },
},
});

module.exports = SemesterEnum;
78 changes: 42 additions & 36 deletions server/schema/company/company.datasources.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
const DataLoader = require('dataloader');
const { APIError } = require('../../utils/exception');
const UserSession = require('../../utils/userAuth/session');
// const UserSession = require('../../utils/userAuth/session');
const CompanyModel = require('./company.model');

const create = async (name, location, logo, session, authToken, mid) => {
try {
const _company = await CompanyModel.create({
name,
location,
logo,
createdBy: UserSession.valid(session, authToken) ? mid : null,
});
return _company;
} catch (error) {
throw APIError(error, { reason: "The company can't be registered" });
}
};

const find = (query, limit, offset) => CompanyModel.find(query).sort({ name: 'asc' }).skip(offset).limit(limit);

const findByID = () =>
new DataLoader(
(ids) => {
async (ids) => {
try {
const _company = ids.map((id) => CompanyModel.findById(id));
return _company;
const _companies = await CompanyModel.find({ _id: ids });
return ids.map((id) => _companies.find((_c) => _c.id.toString() === id.toString()) || null);
} catch (error) {
throw APIError(null, error);
}
Expand All @@ -34,23 +18,45 @@ const findByID = () =>
}
);

const findByIDs = async (ids) => {
try {
const _company = await Promise.all(ids.map((id) => findByID(id)));
return _company;
} catch (error) {
throw APIError(null, error);
}
};
const find = (query, limit, offset) => CompanyModel.find(query).sort({ name: 'asc' }).skip(offset).limit(limit);

// const create = async (name, location, logo, session, authToken, mid) => {
// try {
// const _company = await CompanyModel.create({
// name,
// location,
// logo,
// createdBy: UserSession.valid(session, authToken) ? mid : null,
// });
// return _company;
// } catch (error) {
// throw APIError(error, { reason: "The company can't be registered" });
// }
// };

// const findByIDs = async (ids) => {
// try {
// const _company = await Promise.all(ids.map((id) => findByID(id)));
// return _company;
// } catch (error) {
// throw APIError(null, error);
// }
// };

const deleteById = async (id) => {
try {
return await CompanyModel.findOneAndDelete({ _id: id });
} catch (error) {
throw APIError(error, { reason: "The company can't be deleted" });
}
};
// const deleteById = async (id) => {
// try {
// return await CompanyModel.findOneAndDelete({ _id: id });
// } catch (error) {
// throw APIError(error, { reason: "The company can't be deleted" });
// }
// };

const CompanyDataSources = () => ({ findByID: findByID(), create, find, findByIDs, deleteById });
const CompanyDataSources = () => ({
// create,
// update,
// deleteById,
findByID: findByID(),
find,
});

module.exports = CompanyDataSources;
20 changes: 16 additions & 4 deletions server/schema/company/company.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@ const CompanySchema = new Schema(
],
location: {
type: String,
required: true,
required: false,
trim: true,
},
logo: {
type: Schema.Types.ObjectId,
ref: 'Media',
required: true,
/** [0 - Adamantium Archive A, 1 - Adamantium Archive B, 2 - Active Store] */
store: {
type: Number,
required: false,
min: 0,
max: 2,
},
storePath: {
type: String,
required: false,
},
blurhash: {
type: String,
required: false,
},
},
createdBy: {
type: Schema.Types.ObjectId,
Expand Down
42 changes: 21 additions & 21 deletions server/schema/company/company.mutation.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const { GraphQLObjectType, GraphQLNonNull, GraphQLID, GraphQLString } = require('../scalars');
const { registerCompany, deleteCompanyById } = require('./company.resolver');
const CompanyType = require('./company.type');
const { GraphQLObjectType } = require('../scalars');
// const { registerCompany, deleteCompanyById } = require('./company.resolver');
// const CompanyType = require('./company.type');

module.exports = new GraphQLObjectType({
name: 'CompanyMutation',
fields: {
registerCompany: {
type: CompanyType,
description: 'Register a company',
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
location: { type: new GraphQLNonNull(GraphQLString) },
logo: { type: new GraphQLNonNull(GraphQLID) },
},
resolve: registerCompany,
},
deleteCompanyById: {
type: CompanyType,
description: 'delete comapny by company ID',
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
},
resolve: deleteCompanyById,
},
// registerCompany: {
// type: CompanyType,
// description: 'Register a company',
// args: {
// name: { type: new GraphQLNonNull(GraphQLString) },
// location: { type: new GraphQLNonNull(GraphQLString) },
// logo: { type: new GraphQLNonNull(GraphQLID) },
// },
// resolve: registerCompany,
// },
// deleteCompanyById: {
// type: CompanyType,
// description: 'delete comapny by company ID',
// args: {
// id: { type: new GraphQLNonNull(GraphQLID) },
// },
// resolve: deleteCompanyById,
// },
},
});
41 changes: 21 additions & 20 deletions server/schema/company/company.query.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
const { GraphQLObjectType, GraphQLList, GraphQLNonNull, GraphQLInt, GraphQLID } = require('../scalars');
const { GraphQLObjectType, GraphQLNonNull, GraphQLID } = require('../scalars');
const CompanyType = require('./company.type');

const { getListOfCompanies, getCompanyById, getCompaniesByIds } = require('./company.resolver');
const { getCompanyById } = require('./company.resolver');

module.exports = new GraphQLObjectType({
name: 'CompanyQuery',
fields: {
getListOfCompanies: {
description: 'Get list of all companies',
type: new GraphQLList(CompanyType),
args: {
offset: { type: GraphQLInt },
limit: { type: GraphQLInt },
},
resolve: getListOfCompanies,
},
getCompanyById: {
description: 'Get company by id',
description: 'Get a company by its mongo ID.',
type: CompanyType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
},
resolve: getCompanyById,
},
getCompaniesByIds: {
description: 'Get companies by list of ids',
type: GraphQLList(CompanyType),
args: {
ids: { type: new GraphQLNonNull(GraphQLList(GraphQLID)) },
},
resolve: getCompaniesByIds,
},

// getListOfCompanies: {
// description: 'Get list of all companies',
// type: new GraphQLList(CompanyType),
// args: {
// offset: { type: GraphQLInt },
// limit: { type: GraphQLInt },
// },
// resolve: getListOfCompanies,
// },
// getCompaniesByIds: {
// description: 'Get companies by list of ids',
// type: GraphQLList(CompanyType),
// args: {
// ids: { type: new GraphQLNonNull(GraphQLList(GraphQLID)) },
// },
// resolve: getCompaniesByIds,
// },
},
});
100 changes: 50 additions & 50 deletions server/schema/company/company.resolver.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
const { APIError } = require('../../utils/exception');
const UserPermission = require('../../utils/userAuth/permission');
// const UserPermission = require('../../utils/userAuth/permission');

const DEF_LIMIT = 10;
const DEF_OFFSET = 0;
// const DEF_LIMIT = 10;
// const DEF_OFFSET = 0;

module.exports = {
deleteCompanyById: async (_parent, { id }, { session, authToken, decodedToken, API: { Company } }) => {
try {
if (!UserPermission.exists(session, authToken, decodedToken, 'live.write.all')) {
throw APIError('FORBIDDEN', null, {
reason: 'The user does not have the required permission to perform this operation.',
});
}
const company = await Company.deleteById(id);
return company;
} catch (error) {
throw APIError(null, error);
}
},
getListOfCompanies: async (_parent, { limit = DEF_LIMIT, offset = DEF_OFFSET }, { API: { Company } }) => {
try {
const allCompany = await Company.find({}, limit, offset);
return allCompany;
} catch (error) {
throw APIError(null, error);
}
},
getCompanyById: async (_parent, { id }, { API: { Company } }) => {
getCompanyById: async (_parent, { id }, { API: { Company } }, _) => {
try {
const _company = await Company.findByID.load(id);
return _company;
} catch (error) {
throw APIError(null, error);
}
},
getCompaniesByIds: async (_parent, { ids }, { API: { Company } }) => {
try {
const companies = await Company.findByID.load(ids);
return companies;
} catch (error) {
throw APIError(null, error);
}
},
registerCompany: async (
_parent,
{ name, location, logo },
{ mid, session, authToken, decodedToken, API: { Company } }
) => {
try {
if (!UserPermission.exists(session, authToken, decodedToken, 'live.write.all')) {
throw APIError('FORBIDDEN', null, {
reason: 'The user does not have the required permission to perform this operation.',
});
}
const _company = await Company.create(name, location, logo, session, authToken, mid);
return _company;
} catch (error) {
throw APIError(null, error);
}
},
// deleteCompanyById: async (_parent, { id }, { session, authToken, decodedToken, API: { Company } }) => {
// try {
// if (!UserPermission.exists(session, authToken, decodedToken, 'live.write.all')) {
// throw APIError('FORBIDDEN', null, {
// reason: 'The user does not have the required permission to perform this operation.',
// });
// }
// const company = await Company.deleteById(id);
// return company;
// } catch (error) {
// throw APIError(null, error);
// }
// },
// getListOfCompanies: async (_parent, { limit = DEF_LIMIT, offset = DEF_OFFSET }, { API: { Company } }) => {
// try {
// const allCompany = await Company.find({}, limit, offset);
// return allCompany;
// } catch (error) {
// throw APIError(null, error);
// }
// },
// getCompaniesByIds: async (_parent, { ids }, { API: { Company } }) => {
// try {
// const companies = await Company.findByID.load(ids);
// return companies;
// } catch (error) {
// throw APIError(null, error);
// }
// },
// registerCompany: async (
// _parent,
// { name, location, logo },
// { mid, session, authToken, decodedToken, API: { Company } }
// ) => {
// try {
// if (!UserPermission.exists(session, authToken, decodedToken, 'live.write.all')) {
// throw APIError('FORBIDDEN', null, {
// reason: 'The user does not have the required permission to perform this operation.',
// });
// }
// const _company = await Company.create(name, location, logo, session, authToken, mid);
// return _company;
// } catch (error) {
// throw APIError(null, error);
// }
// },
};
Loading

0 comments on commit 07544fb

Please sign in to comment.