import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { environment } from '../../environments/environment'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ItemService { constructor(private httpClient: HttpClient) { } isItemExists(item): Promise { return this.httpClient.get(`${environment.apiUrl}/api/v1/items/item_name/${item}`).toPromise(); } isSliderExists(slider): Promise { return this.httpClient.get(`${environment.apiUrl}/api/v1/home_slider/home_slider_name/${slider}`).toPromise(); } getAllItems() { return this.httpClient.get(`${environment.apiUrl}/api/v1/items`); } getAllSelectedCategoryItems(categoryName) { return this.httpClient.get(`${environment.apiUrl}/api/v1/items/itemCategories/${categoryName}`); } getAllCustomerItems(){ return this.httpClient.get(`${environment.apiUrl}/api/v1/items/allCustomers`); } /* getCustomerItemsfromStorage() { let customerItems; if (typeof (Storage) !== 'undefined') { customerItems = JSON.parse(localStorage.getItem('customerItems')); } else { console.log('localstorage is not supported so reading directly from server.'); customerItems = this.getAllCustomerItems()['data']['items'] as any[]; } return customerItems; } */ async getHomePageItems(userLoggedIn : boolean,location_id?: number) { if(userLoggedIn) return this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/homepageItems/location/${location_id}`).toPromise(); else return this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/homepageItems/location/defaultlocation`).toPromise(); } getNonkeyandpromoitems(location_id: number) { return this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/nonkeyandpromoitems/location/${location_id}`); } /* converts variant with is_displayed as true to is_default as true and makes the already is_default true variant to is_ddefault as false returns all the such variants with is_displayed true */ getSeparateItemsBasedOnVariantIsDisplayed(itemsToDisplay) { let variantItemsWithDisplay = []; for(let customerItem of itemsToDisplay){ for(let variant of customerItem.variants){ if(variant.is_default != true) { let item = JSON.parse(JSON.stringify(customerItem)); item.variants.find((temp_variant) => { if(temp_variant.is_default === true) { temp_variant.is_default = false; return true; }}); if(item){ item.variants.find((temp_variant) => { if(temp_variant.variant_id === variant.variant_id ) { temp_variant.is_default = true; return true}}); variantItemsWithDisplay.push(item); } } } } return variantItemsWithDisplay; } async getCustomerItems() { return await this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers`).toPromise(); } async getAllCustomerItemVariantsPriceDetails(): Promise { return await this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/prices`).toPromise(); } async getCustomerItemsLastUpdatedDates(): Promise { return await this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/lastUpdatedDate`).toPromise(); } addItem(newItem) { return this.httpClient.post(`${environment.apiUrl}/api/v1/items`, newItem); } addItemWithVariantDetails(newItemWithVariantDetails) { return this.httpClient.post(`${environment.apiUrl}/api/v1/items/addItemWithVariantDetails`, newItemWithVariantDetails); } getProduct(productId) { return this.httpClient.get(`${environment.apiUrl}/api/v1/items/customers/${productId}`) } getTeamItems(){ return this.httpClient.get(`${environment.apiUrl}/api/v1/items/teams`); } getItems(customerStateValue){ if(customerStateValue === 1){ return this.getAllCustomerItems(); }else{ return this.getTeamItems(); } } markAsKeyItem(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/markAsKeyItem`, form); } markAsPromoItem(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/markAsPromoItem`, form); } updateItemAvailability(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/updateItemAvailability`, form); } updateItemActiveStatus(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/updateItemActiveStatus`, form); } updateItemImages(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/updateItemImages`, form); } updateItemDetails(form){ return this.httpClient.put(`${environment.apiUrl}/api/v1/items/updateItemDetails`, form); } }