import { Entity } from './entity'; import { Endereco } from './endereco'; import { Contato } from './contato'; import { Imagem } from './imagem'; import { Dispositivo } from './dispositivo'; import { get } from 'lodash'; export type Classe = "cliente" | "fornecedor" | "transportadora" | "entregador"; export type Emissor = Pessoa & { ambiente_sefaz: string } export class Pessoa extends Entity { cpf_cnpj: string; imagem: Imagem; classe: Classe[] | string; nome: string; razao_social: string; contatos: Array; dispositivos: Array; enderecos: Array; ativo: boolean = true; sexo: string = ''; naturalidade: string = ''; data_operacao: string = ''; data_nascimento: string = ''; tipo: string = ''; rg: string = ''; inscricao_estadual: string = ''; im: string ie: string = ''; inscricao_municipal: string = ''; identificador: string = ''; image_url: string = ''; mobile_image_url: string = ''; constructor(json?: any, generateId?: boolean) { super(json, generateId); json = json || {}; this.cpf_cnpj = json.cpf_cnpj || ''; this.rg = json.rg || ''; this.imagem = json.imagem || get(json, 'dispositivos[0].img') as Imagem; this.classe = json.classe || this.classe; this.image_url = json.image_url || ''; this.mobile_image_url = json.mobile_image_url || ''; this.nome = json.nome || ''; this.identificador = json.identificador || ''; this.razao_social = json.razao_social || ''; this.contatos = json.contatos && new Array(...json.contatos) || new Array(); this.enderecos = json.enderecos && new Array(...json.enderecos) || new Array(); this.ativo = json.ativo || this.ativo; this.dispositivos = json.dispositivos && new Array(...json.dispositivos) || new Array(); this.sexo = json && json.sexo || ''; this.naturalidade = json && json.naturalidade || ''; this.data_operacao = json && json.data_operacao || ''; this.data_nascimento = json && json.data_nascimento || ''; this.tipo = json && json.tipo || ''; this.inscricao_estadual = json && json.inscricao_estadual || ''; this.ie = json && json.ie || ''; this.inscricao_municipal = json && json.inscricao_municipal || ''; this.im = json && json.im || ''; } getContatoPrincipal(tipo: 'telefone' | 'email'): Contato & { index?: number } { const index = this.contatos.findIndex(contato => contato && contato.tipo === tipo && contato.principal) if (index != -1) return Object.assign(this.contatos[index], {index}) } setContatoPrincipal(tipo: 'telefone' | 'email', contato: Contato & { index?: number }) { if (!contato) return; const isValidIndex = ( index: number ): boolean => index !== -1 && index !== null && index !== undefined const indexContatoPrincipal: number = isValidIndex(contato.index) ? contato.index : this.contatos.findIndex(i => i && i.tipo === tipo && i.principal) this.contatos.splice(isValidIndex(indexContatoPrincipal) ? indexContatoPrincipal : this.contatos.length, 1, contato) } get email(): Contato & { index?: number } { return this.getContatoPrincipal('email') } get telefone(): Contato & { index?: number } { return this.getContatoPrincipal('telefone') } get endereco(): Endereco & { index?: number } { const index = this.enderecos.findIndex(endereco => endereco.principal) return index != -1 && Object.assign(this.enderecos[index], {index}) } set email(email: Contato & { index?: number }) { this.setContatoPrincipal('email', email) } set telefone(telefone: Contato & { index?: number }) { this.setContatoPrincipal('telefone', telefone) } } export class User extends Pessoa { constructor(json?:any, generateId?:boolean) { super(json, generateId); } }