import { Injectable } from "@angular/core"; import { SharedService } from "../services/sharedService"; import { Address,PaymentRequest, CreateInvoiceDTO,TagDTO, UPSShippingDTO, BankAccount, Invoice, NewInvoiceWithPayment, ShippingDetail, UPSRateDTO, UPSRateResultDTO, MerchantDTO, ShopDTO, Account, Checkout, AccountState, IBase, CheckoutState, ConfigState, ConfigDataDTO, InventoryItem, InventoryState, InvoiceItem, InvoiceState, Media, MediaState, Product, ProductState, ShippingState, Unit, UnitState, User, UserState, Payment, CardProcessDTO, SquarePaymentRslt } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class SharedState { states = new Array(); _merchantRslt!: MerchantDTO; merchantLoaded: boolean = false; merchInvoiceLoaded: boolean = false; _shopRslt!: ShopDTO; shopLoaded: boolean = false; shopInvoiceLoaded: boolean = false; get thcaRestrict() { return ['AL','AR', 'HI', 'ID', 'KS', 'LA', 'OK', 'OR', 'RI', 'UT', 'VI'] } get account() { if (this._shopRslt?.account) { return this._shopRslt.account } else if (this._merchantRslt?.account) { return this._merchantRslt.account } return null } get user() { if (this._merchantRslt?.user) { return this._merchantRslt.user } else if (this._shopRslt?.user) { return this._shopRslt.user } return null } constructor( private readonly shared: SharedService, private readonly accountState: AccountState, private readonly configState: ConfigState, private readonly inventoryState: InventoryState, private readonly invoiceState: InvoiceState, private readonly mediaState: MediaState, private readonly productState: ProductState, private readonly shippingState: ShippingState, private readonly unitState: UnitState, private readonly userState: UserState ) { } saveMerchant(data: IBase, finish: any, error: any) { // this._merchantRslt. if (data instanceof Account) { const found = this._merchantRslt.account._id === data._id if (found) { this.accountState.put(data as Account, (done: Account) => { if (done) { this._merchantRslt.account = new Account(done) if (this._shopRslt?.account) { this._shopRslt.account = new Account(done) } } finish(done) }, (err: any) => { error(err) }) } else { this.accountState.post(data as Account, (done: Account) => { if (done) { this._merchantRslt.account = new Account(done); } // finish(done) }, (err: any) => { error(err) }) } } else if (data instanceof User) { const found = this._merchantRslt.user._id === data._id if (found) { this.userState.put(data as User, (done: User) => { this._merchantRslt.user = new User(done) if (this._shopRslt?.user) { this._shopRslt.user = new User(done); } finish(done) }, (err: any) => { error(err) }) } } // else if (data instanceof Checkout) { // console.log('Saving Merch Checkout') // const index = this._merchantRslt.checkouts.findIndex(f => f._id === data._id) // if (index >= 0) { // this.checkoutState.put(data as Checkout, (done: Checkout) => { // this._merchantRslt.checkouts[index] = new Checkout(done); // finish(done) // }, (err: any) => { // error(err) // }) // } else { // this.checkoutState.post(data as Checkout, (done: Checkout) => { // this._merchantRslt.checkouts.push(new Checkout(done)) // finish(done) // }, (err: any) => { // error(err) // }) // } // } else if (data instanceof ConfigDataDTO) { const index = this._merchantRslt.configs.findIndex(f => f._id === data._id) if (index >= 0) { this.configState.put(data as ConfigDataDTO, (done: ConfigDataDTO) => { this._merchantRslt.configs[index] = new ConfigDataDTO(done) finish(new ConfigDataDTO(done)) }, (err: any) => { error(err) }) } else { this.configState.post(data as ConfigDataDTO, (done: ConfigDataDTO) => { this._merchantRslt.configs.push(new ConfigDataDTO(done)) finish(new ConfigDataDTO(done)) }, (err: any) => { error(err) }) } } else if (data instanceof InventoryItem) { const index = this._merchantRslt.inventories.findIndex(f => f._id === data._id) if (index >= 0) { this.inventoryState.put(data as InventoryItem, (done: InventoryItem) => { this._merchantRslt.inventories[index] = new InventoryItem(done) finish(new InventoryItem(done)) }, (err: any) => { error(err) }) } else { this.inventoryState.post(data as InventoryItem, (done: InventoryItem) => { this._merchantRslt.inventories.push(new InventoryItem(done)) finish(new InventoryItem(done)) }, (err: any) => { error(err) }) } } else if (data instanceof Invoice) { const index = this._merchantRslt.invoices.findIndex(f => f._id === data._id) if (index >= 0) { this.invoiceState.put(data as Invoice, (done: Invoice) => { if (done) { this._merchantRslt.invoices[index] = new Invoice(done); finish(new Invoice(done)) } else { finish() } }, (err: any) => { error(err) }) } else { this.createInvoice(data as Invoice, (done: CreateInvoiceDTO) => { if (done && done.invoice) { this._merchantRslt.invoices.push(new Invoice(done.invoice)) finish(done) } else { finish(done) } }, (err: any) => { error(err) }) } } else if (data instanceof Media) { //using media state to store media now... const index = this.mediaState.medias.findIndex(f => f._id === data._id) if (index >= 0) { this.mediaState.put(data as Media, (done: Media) => { this.mediaState.medias[index] = new Media(done); finish(new Media(done)) }, (err: any) => { error(err) }) } else { this.mediaState.post(data as Media, (done: Media) => { // this._merchantRslt.medias.push(new Media(done)) this.mediaState.medias.push(new Media(done)) finish(new Media(done)) }, (err: any) => { error(err) }) } } else if (data instanceof Product) { console.log('Saving Merch Product') const index = this._merchantRslt.products.findIndex(f => f._id === data._id) if (index >= 0) { this.productState.put(data as Product, (done: Product) => { this._merchantRslt.products[index] = new Product(done) finish(new Product(done)) }, (err: any) => { error(err) }) } else { this.productState.post(data as Product, (done: Product) => { this._merchantRslt.products.push(new Product(done)) finish(new Product(done)) }, (err: any) => { error(err) }) } } else if (data instanceof Unit) { const index = this._merchantRslt.units.findIndex(f => f._id === data._id) if (index >= 0) { this.unitState.put(data as Unit, (done: Unit) => { this._merchantRslt.units[index] = new Unit(done); finish(new Unit(done)) }, (err: any) => { error(err) }) } else { this.unitState.post(data as Unit, (done: Unit) => { this._merchantRslt.units.push(new Unit(done)) finish(new Unit(done)) }, (err: any) => { error(err) }) } } else if (data instanceof ShippingDetail) { const index = this._merchantRslt.shippings.findIndex(f => f._id === data._id) if (index >= 0) { this.shippingState.put(data as ShippingDetail, (done: ShippingDetail) => { this._merchantRslt.shippings[index] = new ShippingDetail(done); finish(new ShippingDetail(done)) }, (err: any) => { error(err) }) } else { this.shippingState.post(data as ShippingDetail, (done: ShippingDetail) => { this._merchantRslt.shippings.push(new ShippingDetail(done)) finish(new ShippingDetail(done)) }, (err: any) => { error(err) }) } } } //I may only need to have account and user updates as the shopper can't modify very much saveShop(data: IBase, finish: any, error: any) { if (data instanceof Account) { const found = this._shopRslt.account._id === data._id if (found) { this.accountState.put(data as Account, (done: Account) => { if (done) { this._shopRslt.account = done; if (this._merchantRslt?.account) { this._merchantRslt.account = new Account(done) } } finish(done) }, (err: any) => { error(err) }) } else { this.accountState.post(data as Account, (done: Account) => { if (done) { this._shopRslt.account = done; if (this._merchantRslt?.account) { this._merchantRslt.account = new Account(done) } } finish(done) }, (err: any) => { error(err) }) } } else if (data instanceof User) { const found = this._shopRslt.user._id === data._id if (found) { this.userState.put(data as User, (done: User) => { this._shopRslt.user = done; if (this._merchantRslt?.user) { this._merchantRslt.user = done; } finish() }, (err: any) => { error(err) }) } } else if (data instanceof ConfigDataDTO) { const index = this._shopRslt.configs.findIndex(f => f._id === data._id) if (index >= 0) { this.configState.put(data as ConfigDataDTO, (done: ConfigDataDTO) => { this._shopRslt.configs[index] = done; finish() }, (err: any) => { error(err) }) } else { this.configState.post(data as ConfigDataDTO, (done: ConfigDataDTO) => { this._shopRslt.configs.push(done) finish() }, (err: any) => { error(err) }) } } else if (data instanceof InventoryItem) { const index = this._shopRslt.inventories.findIndex(f => f._id === data._id) if (index >= 0) { this.inventoryState.put(data as InventoryItem, (done: InventoryItem) => { this._shopRslt.inventories[index] = done; finish() }, (err: any) => { error(err) }) } else { this.inventoryState.post(data as InventoryItem, (done: InventoryItem) => { this._shopRslt.inventories.push(done) finish() }, (err: any) => { error(err) }) } } else if (data instanceof Invoice) { console.log('Saving Shop Invoice') const index = this._shopRslt.invoices.findIndex(f => f._id === data._id) if (index >= 0) { this.invoiceState.put(data as Invoice, (done: Invoice) => { if (done) { this._shopRslt.invoices[index] = done; finish(done) } else { finish() } }, (err: any) => { error(err) }) } else { this.createInvoice(data as Invoice, (done: CreateInvoiceDTO) => { if (done) { if(done.invoice){ this._shopRslt.invoices.push(done.invoice) } finish(done) } else { finish() } }, (err: any) => { error(err) }) } } else if (data instanceof Media) { const index = this.mediaState.medias.findIndex(f => f._id === data._id) if (index >= 0) { this.mediaState.put(data as Media, (done: Media) => { this.mediaState.medias[index] = done; finish() }, (err: any) => { error(err) }) } else { this.mediaState.post(data as Media, (done: Media) => { this.mediaState.medias.push(done) finish() }, (err: any) => { error(err) }) } } else if (data instanceof Product) { console.log('Saving Shop Product') const index = this._shopRslt.products.findIndex(f => f._id === data._id) if (index >= 0) { this.productState.put(data as Product, (done: Product) => { this._shopRslt.products[index] = done; finish() }, (err: any) => { error(err) }) } else { this.productState.post(data as Product, (done: Product) => { this._shopRslt.products.push(done) finish() }, (err: any) => { error(err) }) } } else if (data instanceof ShippingDetail) { const index = this._shopRslt.shippings.findIndex(f => f._id === data._id) if (index >= 0) { this.shippingState.put(data as ShippingDetail, (done: ShippingDetail) => { this._shopRslt.shippings[index] = done; finish() }, (err: any) => { error(err) }) } else { this.shippingState.post(data as ShippingDetail, (done: ShippingDetail) => { this._shopRslt.shippings.push(done) finish() }, (err: any) => { error(err) }) } } else if (data instanceof Unit) { const index = this._shopRslt.units.findIndex(f => f._id === data._id) if (index >= 0) { this.unitState.put(data as Unit, (done: Unit) => { this._shopRslt.units[index] = done; finish() }, (err: any) => { error(err) }) } else { this.unitState.post(data as Unit, (done: Unit) => { this._shopRslt.units.push(done) finish() }, (err: any) => { error(err) }) } } } getAllUsers(done: any, error: any) { this.shared.getAllUsers().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } getStates(country: string, done: any, error: any) { if (this.states.length > 0) { done(this.states) } const self = this; this.shared.getStates(country).subscribe({ next(rslt) { if (rslt) { self.states = []; rslt.data.states.forEach((f: any) => { self.states.push(f) }) done(self.states) } }, error(e) { error(e) }, complete() { } }) } getCities(country: string, state: string, done: any, error: any) { this.shared.getCities(country, state).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } thcaStates(done: any, error: any) { this.shared.thcaStates().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } pendingUserData(done: any, error: any) { this.shared.getPendingUserData().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } //all items for affiliates affiliate(done: any, error: any) { this.shared.affiliate().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } updateShopDto(dto: ShopDTO) { if (!this._shopRslt) { this._shopRslt = new ShopDTO() } if (dto.account) { this._shopRslt.account = new Account(dto.account) } if (dto.configs) { dto.configs.forEach(f => { this._shopRslt.configs.push(new ConfigDataDTO(f)) }) } if (dto.inventories) { dto.inventories.forEach(f => { this._shopRslt.inventories.push(new InventoryItem(f)) }) } if (dto.invoices) { dto.invoices.forEach(f => { this._shopRslt.invoices.push(new Invoice(f)) }) } if (dto.isOwner) { this._shopRslt.isOwner = dto.isOwner } if (dto.medias) { dto.medias.forEach(f => { this._shopRslt.medias.push(new Media(f)) }) } if (dto.payments) { dto.payments.forEach(f => { this._shopRslt.payments.push(new Payment(f)) }) } if (dto.products) { dto.products.forEach(f => { this._shopRslt.products.push(new Product(f)) }) } if (dto.shippings) { dto.shippings.forEach(f => { this._shopRslt.shippings.push(new ShippingDetail(f)) }) } if (dto.tagData) { dto.tagData.forEach(f => { this._shopRslt.tagData.push(new TagDTO(f)) }) this._shopRslt.tagData = dto.tagData } if (dto.units) { dto.units.forEach(f => { this._shopRslt.units.push(new Unit(f)) }) this._shopRslt.units = dto.units } if (dto.user) { this._shopRslt.user = new User(dto.user) } if (dto.productUsers) { dto.productUsers.forEach(f => { this._shopRslt.productUsers.push(new User(f)) }) } } updateMerchDto(dto: MerchantDTO) { if (!this._merchantRslt) { this._merchantRslt = new MerchantDTO() } if (dto.linkedAccounts) { dto.linkedAccounts.forEach(f => { this._merchantRslt.linkedAccounts.push(new Account(f)) }) } if (dto.account) { this._merchantRslt.account = new Account(dto.account) } if (dto.configs) { dto.configs.forEach(f => { this._merchantRslt.configs.push(new ConfigDataDTO(f)) }) } if (dto.inventories) { dto.inventories.forEach(f => { this._merchantRslt.inventories.push(new InventoryItem(f)) }) } if (dto.invoices) { dto.invoices.forEach(f => { this._merchantRslt.invoices.push(new Invoice(f)) }) } if (dto.medias) { dto.medias.forEach(f => { this._merchantRslt.medias.push(new Media(f)) }) } if (dto.payments) { dto.payments.forEach(f => { this._merchantRslt.payments.push(new Payment(f)) }) } if (dto.products) { dto.products.forEach(f => { this._merchantRslt.products.push(new Product(f)) }) } if (dto.shippings) { dto.shippings.forEach(f => { this._merchantRslt.shippings.push(new ShippingDetail(f)) }) } if (dto.tagData) { dto.tagData.forEach(f => { this._merchantRslt.tagData.push(new TagDTO(f)) }) } if (dto.units) { dto.units.forEach(f => { this._merchantRslt.units.push(new Unit(f)) }) } if (dto.user) { this._merchantRslt.user = new User(dto.user) } } shopInvoices(done: any, error: any) { const self = this; this.shared.shopInvoices().subscribe({ next(rslt) { if (rslt) { self.shopInvoiceLoaded = true; if (!self._shopRslt) { self._shopRslt = new ShopDTO(); } if (rslt.results.invoices) { self._shopRslt.invoices = rslt.results.invoices; } if (rslt.results.payments) { self._shopRslt.payments = rslt.results.payments } // self._shopRslt.invoices = rslt.results.invoices; // self._shopRslt.payments = rslt.results.payments done() // done(rslt) } }, error(e) { error(e) }, complete() { } }) } merchInvoices(id: string,done: any, error: any) { const self = this; this.shared.merchInvoices(id).subscribe({ next(rslt) { if (rslt) { self.merchInvoiceLoaded = true; if (!self._merchantRslt) { self._merchantRslt = new MerchantDTO(); } if (rslt.results.invoices) { self._merchantRslt.invoices = rslt.results.invoices; } if (rslt.results.payments) { self._merchantRslt.payments = rslt.results.payments } done() // done(rslt) } }, error(e) { error(e) }, complete() { } }) } //general marketplace for retail sales marketplace(id: string, done: any, error: any) { const self = this; this.shared.marketplace(id).subscribe({ next(rslt) { if (rslt) { self.shopLoaded = true; self.updateShopDto(rslt.results) done() // done(rslt) } }, error(e) { error(e) }, complete() { } }) } productData(done: any, error: any) { this.shared.productData().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } merchantAll(done: any, error: any) { this.shared.merchantAll().subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } //merchant data and product list merchant(id: string, done: any, error: any) { const self = this; this.shared.merchant(id).subscribe({ next(rslt) { if (rslt) { self.merchantLoaded = true; self.updateMerchDto(rslt.results) done() // done(rslt) } }, error(e) { error(e) }, complete() { } }) } //wholesale marketplace wholesalemarket(id: string, done: any, error: any) { this.shared.marketplace(id).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } //wholesale marketplace legaldocs(id: string, done: any, error: any) { this.shared.legaldocs(id).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } newInvoice(invoice: Invoice, account: BankAccount, done: any, error: any) { const newinvoice = new NewInvoiceWithPayment(); newinvoice.invoice = invoice; newinvoice.payment = account; this.shared.invoiceNewBankAccount(newinvoice).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } getRateData(shippingDetails: ShippingDetail[], toAddress: Address, done: any, error: any) { const dto = new UPSRateDTO(); dto.shippingDetails = shippingDetails; dto.shipTo = toAddress; this.shared.getRateData(dto).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } getShippingData(shippingDetails: ShippingDetail[], toAddress: Address, level: string, done: any, error: any) { const dto = new UPSShippingDTO(); dto.shippingCode = level; dto.shippingDetails = shippingDetails; dto.shipTo = toAddress; this.shared.getShippingData(dto).subscribe({ next(rslt) { if (rslt) { done(rslt) } }, error(e) { error(e) }, complete() { } }) } createInvoice(invoice: Invoice, done: any, error: any) { const self = this; this.shared.createInvoice(invoice).subscribe({ next(rslt) { if (rslt) { done(rslt.results) } }, error(e) { // if(e.results?.length > 0 && e.message === 'Not enough inventory'){ // e.results.forEach((f:InventoryItem) => { // const inventory = new InventoryItem(f) // const index = self._merchantRslt.inventories.findIndex(fi => fi._id === inventory._id); // if(index >= 0){ // self._merchantRslt.inventories[index] = inventory // } // }) // } error(e) }, complete() { } }) } squarePayment(request:PaymentRequest, done: any, error: any) { this.shared.squarePayment(request).subscribe({ next(rslt) { if (rslt) { done(rslt.results as CardProcessDTO) } else { done(null) } }, error(e) { error(e) }, complete() { } }) } }