'use strict'; const Repository = require('../Repository'); class Company { constructor( companyId , owner = null ){ this._companyId = companyId; this._company = null; this._owner = owner; } async populate_content(){ if(! this._company ){ this._company = await Repository.getCompanyById( this._companyId ); } if(! this._owner ){ this._owner = new User( this._company.owner_id, this ); await this._owner.populate_content(); } } async id(){ return this._companyId; } async owner(){ await this.populate_content(); return this._owner; } async type(){ await this.populate_content(); return this._company.type; } async is_hidden(){ await this.populate_content(); return this._company.is_hidden; } async is_active(){ await this.populate_content(); return this._company.is_active; } async name(){ await this.populate_content(); return this._company.name; } async description(){ await this.populate_content(); return this._company.description; } async createdAt(){ await this.populate_content(); return this._company.createdAt; } // locations( limit: Int , offset : Int ) : [Location]! // locationsCount : Int! // categories : [LocationCategory]! // categoriesCount : Int! // truck_types : [TruckType]! // truck_typesCount : Int! } class User { constructor( userId, company = null ){ this._userId = userId; this._user = null; this._company = company; } async populate_content(){ if(! this._user ){ this._user = await Repository.getUserById( this._userId ); } if( (!this._company) && (this._user.company_id) ){ /// Populate only if company is linked to user this._company = new Company( this._user.company_id, this ); await this._company.populate_content(); } } async id(){ return this._userId; } async company(){ await this.populate_content(); return this._company; } async phone(){ await this.populate_content(); return this._user.phone; } async email(){ await this.populate_content(); return this._user.email; } async name(){ await this.populate_content(); return this._user.name; } async last_name(){ await this.populate_content(); return this._user.last_name; } async job_role(){ await this.populate_content(); return this._user.job_role; } async permissions(){ await this.populate_content(); return this._user.permissions; } async createdAt(){ await this.populate_content(); return this._user.createdAt; } async is_active(){ await this.populate_content(); return this._user.is_active; } async locations(){ return []; } async locationsCount(){ return 0; } } class Account { constructor( userId ){ this._userId = userId; this._sessions = null; this._user = null; } async user(){ if( this._user ){ return this._user; } this._user = new User( this._userId ); return this._user; } async _update_sessions(){ if( this._sessions ){ return; } this._sessions = (await Repository.getSessions( this._userId )).map( (item) => { return { token : item.token, expiration : item.expiration }; } ); } async sessions(){ await this._update_sessions(); return this._sessions; } async sessionsCount(){ await this._update_sessions(); return this._sessions.length; } }; module.exports = { Account, User, Company };