import {HttpClient, HttpHeaders} from '@angular/common/http'; import {Injectable} from '@angular/core'; @Injectable() export class AstuteClientService { headers = { headers: new HttpHeaders().set('Content-Type', 'application/json'), }; private authUrl = 'http://localhost:8080/astutesystem/auth'; private customerUrl = 'http://localhost:8080/astutesystem/customer'; private customerContactUrl = 'http://localhost:8080/astutesystem/customer/contact'; private POUrl = 'http://localhost:8080/astutesystem/po'; private POServiceTypesUrl = 'http://localhost:8080/astutesystem/po/serviceTypes'; private PODetailUrl = 'http://localhost:8080/astutesystem/po/detail'; private invoiceUrl = 'http://localhost:8080/astutesystem/invoice'; private invoiceDetailUrl = 'http://localhost:8080/astutesystem/invoice/detail'; private invoiceGenUrl = 'http://localhost:8080/astutesystem/invoice/generatedInvoice'; private invoicePaymentUrl = 'http://localhost:8080/astutesystem/invoicePayment'; private serviceTypeUrl = 'http://localhost:8080/astutesystem/serviceType'; private sessionString = `?sessionId=${localStorage.getItem('SESSION_ID')}`; constructor(private http: HttpClient) { } // **************************************** AUTH Service methods public login(username: string, password: string): Promise { console.log('*** In login()'); const data = { 'username': username, 'password': password }; return this.http .post(this.authUrl, data) .toPromise() .then(response => { console.log(response['entity']); const name = response['entity'].name; const sessionId = response['entity'].sessionId; if (sessionId != null) { localStorage.setItem('SESSION_ID', sessionId); this.sessionString = `?sessionId=${localStorage.getItem('SESSION_ID')}`; console.log(sessionId); localStorage.setItem('SESSION_USER', name); return sessionId; } else { return null; } }); } public logout() { console.log('*** In logout()'); return this.http .post(`${this.authUrl}/logout${this.sessionString}`, {}) .toPromise() .then(response => { localStorage.removeItem('SESSION_ID'); localStorage.removeItem('SESSION_USER'); return response['entity']; }); } public getSessionId(): string { console.log('*** In getSessionId()'); console.log(localStorage.getItem('SESSION_ID')); return localStorage.getItem('SESSION_ID'); } public getSessionUser(): string { console.log('*** In getSessionUser()'); console.log(localStorage.getItem('SESSION_USER')); return localStorage.getItem('SESSION_USER'); } // **************************************** Customer Service methods public getCustomers(): Promise { console.log('*** In getCustomers()'); const url = `${this.customerUrl}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Customers Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateCustomer(customerId: number, data: any): Promise { console.log('*** In updateCustomer()'); const url = `${this.customerUrl}/${customerId}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Update Customer Failed: ' + message); } }, (reason) => { alert('Update Customer Failed: ' + reason); }) .catch( error => { alert(error); }); } public createCustomer(data: any): Promise { console.log('*** In createCustomer()'); const url = `${this.customerUrl}${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Create Customer Failed: ' + message); } }, (reason) => { alert('Create Customer Failed: ' + reason); }) .catch( error => { alert(error); }); } public deleteCustomer(customerId) { console.log('*** In deleteCustomer()'); const url = `${this.customerUrl}/${customerId}/delete${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Delete Customer Failed: ' + message); } }, (reason) => { alert('Delete Customer Failed: ' + reason); }) .catch( error => { alert(error); }); } // **************************************** Customer Contact Service methods public getCustomerContacts(customerId): Promise { console.log('*** In getCustomerContacts()'); const url = `${this.customerContactUrl}?customerId=${customerId}&sessionId=${localStorage.getItem('SESSION_ID')}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customer Contacts Failed: ' + message); } }, (reason) => { alert('Get Customer Contacts Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateCustomerContact(customerId: string, data: any): Promise { console.log('*** In updateCustomerContact()'); const url = `${this.customerContactUrl}/${customerId}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Update Customer Contacts Failed: ' + message); } }, (reason) => { alert('Update Customer Contacts Failed: ' + reason); }) .catch( error => { alert(error); }); } public deleteCustomerContact(customerId: string, contactId: number): Promise { console.log('*** In deleteCustomerContact()'); const url = `${this.customerContactUrl}/${customerId}/${contactId}/delete${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Delete Customer Contacts Failed: ' + message); } }, (reason) => { alert('Delete Customer Contacts Failed: ' + reason); }) .catch( error => { alert(error); }); } public createCustomerContact(data: any): Promise { console.log('*** In createCustomerContact()'); const url = `${this.customerContactUrl}/${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Create Customer Contacts Failed: ' + message); } }, (reason) => { alert('Create Customer Contacts Failed: ' + reason); }) .catch( error => { alert(error); }); } // **************************************** PO Service methods public getPOs(): Promise { console.log('*** In getPOs()'); const url = `${this.POUrl}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Sales Orders Failed: ' + reason); }) .catch( error => { alert(error); }); } public getPODetail(ponum): Promise { console.log('*** In getPODetail()'); const url = `${this.PODetailUrl}?PONum=${ponum}&sessionId=${localStorage.getItem('SESSION_ID')}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Sales Order Details Failed: ' + reason); }) .catch( error => { alert(error); }); } public updatePO(ponum: string, data: any): Promise { console.log('*** In updatePO()'); const url = `${this.POUrl}/${ponum}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Sales Order Failed: ' + reason); }) .catch( error => { alert(error); }); } public createPO(data: any): Promise { console.log('*** In createPO()'); const url = `${this.POUrl}${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Create Sales Order Failed: ' + reason); }) .catch( error => { alert(error); }); } public updatePODetail(ponum, lineItemNo, data) { console.log('*** In updatePODetail()'); const sessionId = localStorage.getItem('sessionId'); const url = `${this.POUrl}/detail/${ponum}/${lineItemNo}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Sales Order Details Failed: ' + reason); }) .catch( error => { alert(error); }); } public createPODetail(data) { console.log('*** In createPODetail()'); const url = `${this.POUrl}/detail${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Create Sales Order Failed: ' + reason); }) .catch( error => { alert(error); }); } public finalizePO(ponum: string) { console.log('*** In finalizePO()'); const url = `${this.POUrl}/${ponum}/finalize${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Finalize Sales Order Failed: ' + reason); }) .catch( error => { alert(error); }); } public deletePO(ponum: string) { console.log('*** In deletePO()'); const url = `${this.POUrl}/${ponum}/delete${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Delete Sales Order Failed: ' + reason); }) .catch( error => { alert(error); }); } public getRateTypes(): Promise { console.log('*** In getRateTypes()'); const url = `${this.POUrl}/feeTypes${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Rate Types Failed: ' + reason); }) .catch( error => { alert(error); }); } // **************************************** Invoice Service methods public submitInvoice (invoiceNumber) { console.log('*** In submitInvoice(), invoiceNumber' + invoiceNumber); const url = `${this.invoiceUrl}/${invoiceNumber}/submit${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Submit Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public voidInvoice (invoiceNumber) { console.log('*** In voidInvoice(), invoiceNumber' + invoiceNumber); const url = `${this.invoiceUrl}/${invoiceNumber}/void${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Void Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public deleteInvoice (invoiceNumber) { console.log('*** In deleteInvoice(), invoiceNumber' + invoiceNumber); const url = `${this.invoiceUrl}/${invoiceNumber}/delete${this.sessionString}`; return this.http.put(url, {}) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Delete Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public generateInvoiceNumber (ponum) { console.log('*** In generateInvoiceNumber()'); const url = `${this.invoiceUrl}/generateInvoiceNumber/${ponum}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Generate Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public getInvoices(): Promise { console.log('*** In getInvoices()'); const url = `${this.invoiceUrl}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Invoices Failed: ' + reason); }) .catch( error => { alert(error); }); } public getInvoiceDetail(invoiceId: string): Promise { console.log('*** In getInvoiceDetail()'); const url = `${this.invoiceDetailUrl}?invoiceNumber=${invoiceId}&sessionId=${localStorage.getItem('SESSION_ID')}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Invoice Details Failed: ' + reason); }) .catch( error => { alert(error); }); } public getInvoiceGen (invoiceId: string): Promise { console.log('*** In getInvoiceGen()'); const url = `${this.invoiceGenUrl}/${invoiceId}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Generated Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateInvoice(invoiceNumber: string, data: any): Promise { console.log('*** In updateInvoice()'); const url = `${this.invoiceUrl}/${invoiceNumber}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public createInvoice(data: any): Promise { console.log('*** In createInvoice()'); const url = `${this.invoiceUrl}${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Create Invoice Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateInvoiceDetail(invNum, lineItemNo, data) { console.log('*** In updateInvoiceDetail()'); const url = `${this.invoiceUrl}/detail/${invNum}/${lineItemNo}${this.sessionString}`; return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Invoice Detail Failed: ' + reason); }) .catch( error => { alert(error); }); } public createInvoiceDetail(data) { console.log('*** In createInvoiceDetail()'); const url = `${this.invoiceUrl}/detail${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Create Invoice Detail Failed: ' + reason); }) .catch( error => { alert(error); }); } // **************************************** Invoice Payment Service methods public getSumittedInvoices(): Promise { console.log('*** In getSumittedInvoices()'); const url = `${this.invoiceUrl}/submitted${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Submitted Invoices Failed: ' + reason); }) .catch( error => { alert(error); }); } public getInvoicePaymentTypes(): Promise { console.log('*** In getInvoicePaymentTypes()'); const url = `${this.invoicePaymentUrl}/paymentTypes${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Invoice Payment Types Failed: ' + reason); }) .catch( error => { alert(error); }); } public getInvoicePayments(): Promise { console.log('*** In getInvoicePayments()'); const url = `${this.invoicePaymentUrl}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Invoice Payments Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateInvoicePayment(invoiceNumber: string, invoicePaymentId: string, data: any): Promise { console.log('*** In updateInvoicePayment()'); const url = `${this.invoicePaymentUrl}/${invoiceNumber}/${invoicePaymentId}${this.sessionString}`; console.log('*** invoicePaymentUrl is ' + url); return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Invoice Payment Failed: ' + reason); }) .catch( error => { alert(error); }); } public addInvoicePayment(data: any): Promise { console.log('*** In addInvoicePayment()'); const url = `${this.invoicePaymentUrl}${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Add Invoice Payment Failed: ' + reason); }) .catch( error => { alert(error); }); } // **************************************** Service Type methods public getServiceTypes(): Promise { console.log('*** In getServiceTypes()'); const url = `${this.serviceTypeUrl}${this.sessionString}`; return this.http.get(url) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Get Service Types Failed: ' + reason); }) .catch( error => { alert(error); }); } public updateServiceType(serviceTypeId, data: any): Promise { console.log('*** In updateServiceType()'); const url = `${this.serviceTypeUrl}/${serviceTypeId}${this.sessionString}`; console.log('*** updateServiceType is ' + url); return this.http.put(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Update Service Type Failed: ' + reason); }) .catch( error => { alert(error); }); } public createServiceType(data: any): Promise { console.log('*** In createServiceType()'); const url = `${this.serviceTypeUrl}${this.sessionString}`; return this.http.post(url, data) .toPromise() .then(response => { const code = response['code']; const message = response['message']; if (code === 9000) { console.log(response['entity']); return response['entity']; } else if (message.includes('login')) { alert('Please login again!'); } else { alert('Get Customers Failed: ' + message); } }, (reason) => { alert('Create Service Type Failed: ' + reason); }) .catch( error => { alert(error); }); } }