All files / src/entities user.ts

59.09% Statements 104/176
0% Branches 0/21
5% Functions 1/20
60.71% Lines 102/168

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426                      1x                                     1x               1x     1x   1x 1x 1x 1x   1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x                                                                               1x               1x                           1x       1x       1x       1x                                     1x                         1x               1x                         1x  
import { Address } from './address.js';
import { Assignment } from './assignment.js';
import { Cart } from './cart.js';
import { Category } from './category.js';
import { CompanyInvitation } from './company_invitation.js';
import { DomainInvitation } from './domain_invitation.js';
import { Draft } from './draft.js';
import { DraftComment } from './draft_comment.js';
import { EmailAddress } from './email_address.js';
import { EnrolledDomain } from './enrolled_domain.js';
import { Domain } from './domain.js';
import { Entity } from '../entity.js';
import { MerchiFile } from './file.js';
import { Invoice } from './invoice.js';
import { Job } from './job.js';
import { JobComment } from './job_comment.js';
import { JobOperationLog } from './job_operation_log.js';
import { Notification } from './notification.js';
import { Payment } from './payment.js';
import { PhoneNumber } from './phone_number.js';
import { Product } from './product.js';
import { Reminder } from './reminder.js';
import { ProductionComment } from './production_comment.js';
import { Session } from './session.js';
import { Shipment } from './shipment.js';
import { ShipmentLog } from './shipment_log.js';
import { SystemRole } from './system_role.js';
import { Theme } from './theme.js';
import { UserCompany } from './user_company.js';
import { Company } from './company.js';
import { Role,
  DOMAIN_MANAGERS,
  MANAGEMENT_TEAM,
  BUSINESS_ACCOUNTS,
  ROLES_RANK
} from '../constants/roles.js';
import { InternalTag } from './internal_tag.js';
import { DomainTag } from './domain_tag.js';
import { UserType } from '../constants/user_types.js';
import { SystemRoles as SR } from '../constants/system_roles.js';
 
import { some } from 'lodash';
 
export class User extends Entity {
  protected static resourceName = 'users';
  protected static singularName = 'user';
  protected static pluralName = 'users';
 
  @User.property({type: Date})
  public archived?: Date | null;
 
  @User.property()
  public id?: number;
 
  @User.property()
  public isSuperUser?: boolean;
 
  @User.property()
  public userType?: UserType;
 
  @User.property()
  public registeredAsGuest?: boolean;
 
  @User.property({arrayType: 'InternalTag'})
  public internalTags?: InternalTag[];
 
  @User.property({arrayType: 'DomainTag'})
  public tags?: DomainTag[];
 
  @User.property({arrayType: 'Domain'})
  public registeredUnderDomains?: Domain[];
 
  @User.property({type: String})
  public password?: string | null;
 
  @User.property({type: String})
  public salt?: string | null;
 
  @User.property({type: String})
  public facebookUserId?: string | null;
 
  @User.property({type: String})
  public resetToken?: string | null;
 
  @User.property({type: String})
  public resetTokenDate?: Date | null;
 
  @User.property({type: String})
  public smsToken?: string | null;
 
  @User.property({type: Date})
  public resetSmsTokenDate?: Date | null;
 
  @User.property({type: String})
  public smsClientToken?: string | null;
 
  @User.property({type: Boolean})
  public smsTokenConfirmed?: boolean;
 
  @User.property({type: Number})
  public smsLoginThreshold?: number;
 
  @User.property()
  public jobCountAsClient?: number;
 
  @User.property()
  public totalInvoiceValue?: number;
 
  @User.property()
  public hasStore?: boolean;
 
  @User.property()
  public lastJobReceived?: boolean;
 
  @User.property()
  public canEdit?: boolean;
 
  @User.property()
  public enableCrashReports?: boolean;
 
  @User.property()
  public enableClientEmails?: boolean;
 
  @User.property({type: String})
  public clientToken?: string | null;
 
  @User.property()
  public name?: string;
 
  @User.property({type: String})
  public internalUseNotes?: string;
 
  @User.property({type: String})
  public telegramUsername?: string;
 
  @User.property({type: String})
  public internalUseAiContext?: string;
 
  @User.property({type: String})
  public aiContext?: string;
 
  @User.property()
  public callToActions?: string;
 
  @User.property()
  public callToActionDetails?: any[];
 
  @User.property({type: String})
  public comments?: string | null;
 
  @User.property({type: String})
  public timezone?: string | null;
 
  @User.property({type: Date})
  public created?: Date | null;
 
  @User.property({type: String})
  public preferredLanguage?: string | null;
 
  @User.property()
  public enableInvoiceReminders?: boolean;
 
  @User.property()
  public enableStoreNotifications?: boolean;
 
  @User.property()
  public isAdminOfSubscribedCompany?: boolean;
 
  @User.property({ arrayType: 'JobComment' })
  public jobComments?: JobComment[];
 
  @User.property({ arrayType: 'JobOperationLog' })
  public jobOperationLogs?: JobOperationLog[];
 
  @User.property({ arrayType: 'ShipmentLog' })
  public shipmentLogs?: ShipmentLog[];
 
  @User.property({ arrayType: 'EmailAddress' })
  public _emailAddresses?: EmailAddress[];
 
  @User.property({ arrayType: 'PhoneNumber' })
  public _phoneNumbers?: PhoneNumber[];
 
  @User.property({ arrayType: 'Address' })
  public _addresses?: Address[];
 
  @User.property({ arrayType: 'UserCompany' })
  public _companies?: UserCompany[];
 
  @User.property({ arrayType: 'Company' })
  public companiesNeedStripe?: Company[];
 
  @User.property({ arrayType: 'Category' })
  public categories?: Category[];
 
  @User.property({ arrayType: 'Product' })
  public products?: Product[];
 
  @User.property({type: 'MerchiFile'})
  public profilePicture?: MerchiFile | null;
 
  @User.property({ arrayType: 'PhoneNumber' })
  public phoneNumbers?: PhoneNumber[];
 
  @User.property({ arrayType: 'Session' })
  public sessions?: Session[];
 
  @User.property({ arrayType: 'Shipment' })
  public shipmentsAsSender?: Shipment[];
 
  @User.property({ arrayType: 'Shipment' })
  public shipmentsAsReceiver?: Shipment[];
 
  @User.property({ arrayType: 'DraftComment' })
  public draftComments?: DraftComment[];
 
  @User.property({ arrayType: 'DraftComment' })
  public forwardedDraftComments?: DraftComment[];
 
  @User.property({ arrayType: 'SystemRole' })
  public systemRoles?: SystemRole[];
 
  @User.property({ arrayType: 'EmailAddress' })
  public emailAddresses?: EmailAddress[];
 
  @User.property({ arrayType: 'Notification' })
  public notifications?: Notification[];
 
  @User.property({ arrayType: 'Notification' })
  public sentNotifications?: Notification[];
 
  @User.property({ arrayType: 'Reminder' })
  public reminders?: Reminder[];
 
  @User.property({ arrayType: 'Assignment' })
  public assignments?: Assignment[];
 
  @User.property({ arrayType: 'UserCompany' })
  public userCompanies?: UserCompany[];
 
  @User.property({ arrayType: 'Draft' })
  public drafts?: Draft[];
 
  @User.property({ arrayType: 'CompanyInvitation' })
  public companyInvitations?: CompanyInvitation[];
 
  @User.property({ arrayType: 'Address' })
  public addresses?: Address[];
 
  @User.property({ arrayType: 'MerchiFile' })
  public uploadFiles?: MerchiFile[];
 
  @User.property({ arrayType: 'JobComment' })
  public forwardedJobComments?: JobComment[];
 
  @User.property({ arrayType: 'Job' })
  public appliedJobs?: Job[];
 
  @User.property({ arrayType: 'Job' })
  public managedJobs?: Job[];
 
  @User.property({ arrayType: 'Job' })
  public draftingJobs?: Job[];
 
  @User.property({ arrayType: 'Product' })
  public saved_products?: Product[];
 
  @User.property({ arrayType: 'Cart' })
  public carts?: Cart[];
 
  @User.property({ arrayType: 'Payment' })
  public payments?: Payment[];
 
  @User.property({ arrayType: 'EnrolledDomain' })
  public enrolledDomains?: EnrolledDomain[];
 
  @User.property({ arrayType: 'Invoice' })
  public responsibleInvoices?: Invoice[];
 
  @User.property({ arrayType: 'Invoice' })
  public createdInvoices?: Invoice[];
 
  @User.property({ arrayType: 'Invoice' })
  public invoicesHas?: Invoice[];
 
  @User.property({ arrayType: 'DomainInvitation' })
  public sentDomainInvitations?: DomainInvitation[];
 
  @User.property({ arrayType: 'DomainInvitation' })
  public receivedDomainInvitations?: DomainInvitation[];
 
  @User.property({ arrayType: 'Theme' })
  public themes?: Theme[];
 
  @User.property({ arrayType: 'ProductionComment' })
  public forwardedProductionComments?: ProductionComment[];
 
  @User.property({ arrayType: 'Domain' })
  public accessibleDomainsAsClient?: Domain[];
 
  public publicCreate = this.createFactory(
    {resourceName: 'public_user_create'});
 
  public roleInDomain = (domain: Domain): Role => {
    Iif (this.enrolledDomains === undefined) {
      const err = 'enrolledDomains is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    Iif (
      this.enrolledDomains
        .map(enrolledDomain => enrolledDomain.domain)
        .some(domain => domain === undefined)
    ) {
      const err =
        'Domain of enrolled domain is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    const matchingEnrolledDomains = this.enrolledDomains.filter(
      enrolledDomain => enrolledDomain.domain && enrolledDomain.domain.id === domain.id);
 
    Iif (matchingEnrolledDomains.length > 1) {
      const highestRoleRank = Math.max(
        ...matchingEnrolledDomains.map(ed => ROLES_RANK.findIndex(e => e === ed.role)));
      return ROLES_RANK[highestRoleRank];
    }
 
    // due to some current system issues, user can have multiple enrolled domain
    // of one domain, if that is the case either of them can be consider as
    // valid, need to revisit it in the future related to #
    Iif (matchingEnrolledDomains.length > 0) {
      Iif (matchingEnrolledDomains[0].role !== undefined) {
        return matchingEnrolledDomains[0].role;
      }
    }
    return Role.PUBLIC;
  };
 
  public isSuper(): boolean {
    Iif (this.isSuperUser === undefined) {
      const err = 'isSuperUser is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    return this.isSuperUser!;
  }
 
  public companies(): Company[] {
    Iif (this.userCompanies === undefined) {
      const err = 'userCompanies is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    return this.userCompanies.map((userCompany) => {
      Iif (userCompany.company === undefined) {
        const err = 'userCompany.company is undefined, did you forget to embed it?';
        throw new Error(err);
      }
      return userCompany.company!;
    });
  }
 
  public isDomainManager(domain: Domain) {
    return DOMAIN_MANAGERS.includes(this.roleInDomain(domain));
  }
 
  public isManagementTeam(domain: Domain) {
    return MANAGEMENT_TEAM.includes(this.roleInDomain(domain));
  }
 
  public isNotClient(domain: Domain) {
    return BUSINESS_ACCOUNTS.includes(this.roleInDomain(domain));
  }
 
  public domainsByRoles(roles: Role[]) {
    Iif (this.enrolledDomains === undefined) {
      const err = 'enrolledDomains is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    const result = [];
    for (let i = 0; i < this.enrolledDomains.length; ++i) {
      const domain = this.enrolledDomains[i].domain;
      Iif (domain === undefined) {
        const err = 'domain is undefined, did you forget to embed it?';
        throw new Error(err);
      }
      Iif (this.hasAuthority(domain, roles)) {
        result.push(domain);
      }
    }
    return result;
  }
 
  public hasAuthority(domain: Domain, roles: Role[]) {
    Iif (this.isSuper()) {
      return true;
    }
    const role = this.roleInDomain(domain);
    for (let i = 0; i < roles.length; ++i) {
      Iif (roles[i] === role) {
        return true;
      }
    }
    return false;
  }
 
  public hasSystemRole(role: SR): boolean {
    Iif (this.systemRoles === undefined) {
      const err = 'systemRoles is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    return some(this.systemRoles.map(systemRole => systemRole.role === role));
  }
 
  public hasRoles(roles: Role[], IcombinationMethod = some) {
    Iif (this.enrolledDomains === undefined) {
      const err = 'enrolledDomains is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    const allRoles = this.enrolledDomains.map(
      enrolledDomain => enrolledDomain.getRole()
    );
    allRoles.push(Role.PUBLIC);
    return combinationMethod(
      roles.map((role) => allRoles.indexOf(role) !== -1)
    );
  }
}