feat: Adding basic GraphQL elements (company,user,sessions)

This commit is contained in:
Josepablo C
2024-08-06 12:19:53 -06:00
parent ae920ca2c7
commit 6b4c475a38
7 changed files with 385 additions and 39 deletions

View File

@@ -1,6 +1,5 @@
"use strict";
const { ROOT_PATH, HANDLERS_PATH } = process.env;
const { complete_register } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Account` );
const { complete_register } = require( '../../../lib/Handlers/Account' );
const register = async( req, res ) => {
try{

View File

@@ -1,6 +1,6 @@
'use strict';
const { DateResolver, DateTimeResolver } = require('graphql-scalars');
const { Account, User, Company } = require('../../Domain');
const { Account, User, Company, getUserById, getCompanyById, findUsersPage, findCompaniesPage } = require('../../Domain');
//////////////////////////////////////////////
// Queries
@@ -20,12 +20,34 @@ async function company( args, context ) {
return company;
}
//////////////////////////////////////////////
async function companyById( args, context ) {
return getCompanyById( args.id );
}
async function userById( args, context ) {
return getUserById( args.id );
}
async function findCompanies( args, context ) {
const { filters, elements, page } = args;
return findCompaniesPage( filters, elements, page );
}
async function findUsers( args, context ) {
const { filters, elements, page } = args;
return findUsersPage( filters, elements, page );
}
/////////////////////////////////////////////////
// Mutations
//////////////////////////////////////////////
/////////////////////////////////////////////////
module.exports = {
account,
profile,
company
company,
companyById,
userById,
findCompanies,
findUsers
};

View File

@@ -2,6 +2,12 @@ type Query {
account : Account!
profile : User!
company : Company!
companyById( id : Int! ) : PublicCompany
userById( id : Int! ) : PublicUser
findCompanies( filters : FindCompanyFilterInput!, elements: Int!, page: Int! ) : PublicCompanyFound!
findUsers( filters : FindUserFilterInput!, elements: Int!, page: Int! ) : PublicUserFound!
}
scalar DateTime
@@ -15,7 +21,6 @@ type Account {
user : User!
sessions( limit: Int , offset : Int ) : [Session]!
sessionsCount : Int!
}
type LocationCategory{
@@ -35,7 +40,6 @@ type Location{
address_line2 : String
categories : [LocationCategory]!
categoriesCount : Int!
}
type TruckType{
@@ -51,6 +55,8 @@ type CompanyCategory{
type Company {
id : Int!
owner : User!
staff : [User]!
type : String!
is_hidden : Boolean!
is_active : Boolean!
@@ -60,14 +66,9 @@ type Company {
createdAt : DateTime!
locations( limit: Int , offset : Int ) : [Location]!
locationsCount : Int!
categories : [LocationCategory]!
categoriesCount : Int!
locations : [Location]!
categories : [CompanyCategory]!
truck_types : [TruckType]!
truck_typesCount : Int!
}
type User {
@@ -84,5 +85,62 @@ type User {
is_active : Boolean!
locations( limit: Int , offset : Int ) : [Location]!
locationsCount : Int!
}
type PublicCompany {
id : Int!
owner : PublicUser!
staff : [PublicUser]!
type : String!
name : String!
description : String
createdAt : DateTime!
locations : [Location]!
categories : [CompanyCategory]!
truck_types : [TruckType]!
}
type PublicCompanyFound {
count : Int!
list : [PublicCompany]!
}
input FindCompanyFilterInput {
name : String
type : String
state : String
city : String
truck_type : String
categories : String
}
type PublicUser {
id : Int!
company : Company
phone : String
email : String!
name : String!
last_name : String!
job_role : String!
createdAt : DateTime!
locations( limit: Int , offset : Int ) : [Location]!
}
type PublicUserFound {
count : Int!
list : [PublicUser]!
}
input FindUserFilterInput {
email : String
name : String
last_name : String
employee_id : String
company_name : String
job_role : String
}

View File

@@ -2,11 +2,100 @@
const Repository = require('../Repository');
class TruckType {
constructor( category ){
this._category = category;
}
async id(){
return this._category.id;
}
async category(){
return this._category.category;
}
}
class Category {
constructor( category ){
this._category = category;
}
async id(){
return this._category.id;
}
async category(){
return this._category.category;
}
}
class Location {
constructor( location ){
this._location = location;
this._categories = null;
}
async populate_content(){
if( !this._categories ){
this._categories = await Repository.getCategories( 'location', this._location.id );
this._categories.map( (element) => {
return new Category( element );
});
}
}
async company_id(){
await this.populate_content();
this._location.company_id;
}
async type(){
await this.populate_content();
this._location.type;
}
async state(){
await this.populate_content();
this._location.state;
}
async city(){
await this.populate_content();
this._location.city;
}
async country(){
await this.populate_content();
this._location.country;
}
async zipcode(){
await this.populate_content();
this._location.zipcode;
}
async address_line1(){
await this.populate_content();
this._location.address_line1;
}
async address_line2(){
await this.populate_content();
this._location.address_line2;
}
async categories(){
await this.populate_content();
return this._categories;
}
}
class Company {
constructor( companyId , owner = null ){
this._companyId = companyId;
this._company = null;
this._owner = owner;
this._staff = null;
this._locations = null;
this._truck_types = null;
this._categories = null;
}
setCompany( company ){
this._company = company;
}
async populate_content(){
@@ -17,6 +106,31 @@ class Company {
this._owner = new User( this._company.owner_id, this );
await this._owner.populate_content();
}
if( !this._locations ){
this._locations = await Repository.getLocations( 'company', this._companyId );
this._locations.map( (element) => {
return new Location( element );
});
}
if( !this._truck_types ){
this._truck_types = await Repository.getTruckTypes( 'company', this._companyId );
this._truck_types.map( (element) => {
return new TruckType( element );
});
}
if( !this._categories ){
this._categories = await Repository.getCategories( 'company', this._companyId );
this._categories.map( (element) => {
return new Category( element );
});
}
if( !this._staff ){
/// ToDo: Populate _staff
this._staff = []
}
}
async id(){
@@ -26,6 +140,10 @@ class Company {
await this.populate_content();
return this._owner;
}
async staff(){
await this.populate_content();
return this._staff;
}
async type(){
await this.populate_content();
@@ -52,22 +170,33 @@ class Company {
return this._company.createdAt;
}
// locations( limit: Int , offset : Int ) : [Location]!
// locationsCount : Int!
// categories : [LocationCategory]!
// categoriesCount : Int!
// truck_types : [TruckType]!
// truck_typesCount : Int!
async locations(){
await this.populate_content();
return this._locations;
}
async categories(){
await this.populate_content();
return this._categories;
}
async truck_types(){
await this.populate_content();
return this._truck_types;
}
}
class User {
constructor( userId, company = null ){
this._userId = userId;
this._user = null;
this._locations = null;
this._company = company;
}
setUser( user ){
this._user = user;
}
async populate_content(){
if(! this._user ){
this._user = await Repository.getUserById( this._userId );
@@ -78,6 +207,14 @@ class User {
this._company = new Company( this._user.company_id, this );
await this._company.populate_content();
}
if( !this._locations ){
this._locations = await Repository.getLocations( 'user', this._userId );
this._locations.map( (element) => {
return new Location( element );
});
}
}
async id(){
@@ -123,11 +260,8 @@ class User {
}
async locations(){
return [];
}
async locationsCount(){
return 0;
await this.populate_content();
return this._locations;
}
}
@@ -138,6 +272,10 @@ class Account {
this._user = null;
}
setUser( user ){
this._user = user;
}
async user(){
if( this._user ){
return this._user;
@@ -147,12 +285,14 @@ class Account {
return this._user;
}
async _update_sessions(){
async populate_content(){
if( this._sessions ){
return;
}
this._sessions = (await Repository.getSessions( this._userId )).map( (item) => {
this._sessions = await Repository.getSessions( this._userId );
this._sessions.map( (item) => {
return {
token : item.token,
expiration : item.expiration
@@ -161,19 +301,70 @@ class Account {
}
async sessions(){
await this._update_sessions();
await this.populate_content();
return this._sessions;
}
async sessionsCount(){
await this._update_sessions();
return this._sessions.length;
}
};
async function getUserById( id ) {
const user = await Repository.getUserById( id );
if( user ){
const user_obj = new User( user.id );
user_obj.setUser(user);
return user_obj;
}else{
return null;
}
}
async function getCompanyById( id ) {
const company = await Repository.getCompanyById( id );
if( company ){
const company_obj = new Company( user.id );
company_obj.setCompany(company);
return company_obj;
}else{
return null;
}
}
async function findUsersPage( filters, elements, page ) {
/// ToDo: Populate find
const list = [];
list.map( (element) => {
const user = new User( element.id );
user.setUser( element );
return user;
} );
return {
count : list.length,
list
};
}
async function findCompaniesPage( filters, elements, page ) {
/// ToDo: Populate find
const list = [];
list.map( (element) => {
const company = new Company( element.id );
company.setCompany( element );
return company;
} );
return {
count : list.length,
list
};
}
module.exports = {
Account,
User,
Company
Company,
Location,
Category,
TruckType,
getUserById,
getCompanyById,
findUsersPage,
findCompaniesPage,
};

View File

@@ -5,6 +5,13 @@ const Users = getModel('users');
const Sessions = getModel('users_sessions');
const Companies = getModel('companies');
const Locations = getModel('locations');
const userLocations = getModel('user_locations');
const companyTruckTypes = getModel('company_truck_types');
const companyCategories = getModel('company_categories');
const locationTruckTypes = getModel('location_truck_types');
const locationCategories = getModel('location_categories');
class SpecificModelRepository{
constructor(){}
@@ -20,6 +27,42 @@ class SpecificModelRepository{
return await Sessions.query().where("user_id","=",userId);
}
async getLocations( location_type, elementId ){
switch( location_type ){
case 'company':
return await Locations.query().where("company_id","=",elementId);
case 'user':
///ToDo: Implement join on Locations and userLocations
return [];
default:
return [];
}
}
async getCategories( category_type, elementId ){
switch( category_type ){
case 'company':
return await companyCategories.query().where("company_id","=",elementId);
case 'location':
///ToDo: Implement join on companyCategories and locationCategories
return [];
default:
return [];
}
}
async getTruckTypes( search_type, elementId ){
switch( search_type ){
case 'company':
return await companyTruckTypes.query().where("company_id","=",elementId);
case 'location':
///ToDo: Implement join on companyTruckTypes and locationTruckTypes
return [];
default:
return [];
}
}
}
module.exports = new SpecificModelRepository();

View File

@@ -3,6 +3,17 @@
const users = require('./users.model');
const user_sessions = require('./user_sessions.model');
const companies = require('./companies.model');
const locations = require('./locations.model');
const user_locations = require('./user_locations.model');
const company_truck_types = require('./company_truck_types.model');
const company_categories = require('./company_categories.model');
const location_categories = require('./location_categories.model');
const location_truck_types = require('./location_truck_types.model');
const metadata_categories = require('./metadata_categories.model');
const metadata_cities = require('./metadata_cities.model');
const metadata_products = require('./metadata_products.model');
const metadata_truck_types = require('./metadata_truck_types.model');
function getModel( name ){
switch( name ){
@@ -12,6 +23,28 @@ function getModel( name ){
return user_sessions;
case 'companies':
return companies;
case 'locations':
return locations;
case 'user_locations':
return user_locations;
case 'company_truck_types':
return company_truck_types;
case 'company_categories':
return company_categories;
case 'location_categories':
return location_categories;
case 'location_truck_types':
return location_truck_types;
case 'metadata_categories':
return metadata_categories;
case 'metadata_cities':
return metadata_cities;
case 'metadata_products':
return metadata_products;
case 'metadata_truck_types':
return metadata_truck_types;
default:
return null;
}

View File

@@ -22,4 +22,4 @@ class Locations extends Model {
}
}
module.exports = Users;
module.exports = Locations;