102 lines
3.3 KiB
TypeScript
102 lines
3.3 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import { map, Observable } from 'rxjs';
|
|
import { environment } from '../../../../environments/environment';
|
|
import { Contact } from '../../models/service-provider/contact';
|
|
import { UserService } from '../common/user.service';
|
|
import { ContactLogin } from '../../models/service-provider/contact-login';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ContactService {
|
|
private apiUrl = environment.apiUrl;
|
|
private apiDb = environment.apiDb;
|
|
|
|
private http = inject(HttpClient);
|
|
private userService = inject(UserService);
|
|
|
|
getContactsById(id: number): Observable<Contact[]> {
|
|
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetSPAllContacts/${id}`).pipe(
|
|
map(response => this.mapToContacts(response)));
|
|
}
|
|
|
|
private mapToContacts(data: any[]): Contact[] {
|
|
return data.map(contact => ({
|
|
spContactId: contact.SPCONTACTID,
|
|
serviceProviderId: contact.SPID,
|
|
defaultContact: contact.DEFCONTACTFLAG === 'Y',
|
|
firstName: contact.FIRSTNAME,
|
|
lastName: contact.LASTNAME,
|
|
title: contact.TITLE,
|
|
phone: contact.PHONENO,
|
|
mobile: contact.MOBILENO,
|
|
fax: contact.FAXNO || null,
|
|
email: contact.EMAILADDRESS,
|
|
middleInitial: contact.MIDDLEINITIAL || null,
|
|
createdBy: contact.CREATEDBY || null,
|
|
dateCreated: contact.DATECREATED || null,
|
|
lastUpdatedBy: contact.LASTUPDATEDBY || null,
|
|
lastUpdatedDate: contact.LASTUPDATEDDATE || null,
|
|
isInactive: contact.INACTIVEFLAG === 'Y' || false,
|
|
inactivatedDate: contact.INACTIVEDATE || null,
|
|
hasLogin: contact.LOGINFLAG === 'Y' || false
|
|
}));
|
|
}
|
|
|
|
createContact(spid: number, data: Contact): Observable<any> {
|
|
|
|
const contact = {
|
|
p_spid: spid,
|
|
p_defcontactflag: data.defaultContact ? 'Y' : 'N',
|
|
p_firstname: data.firstName,
|
|
p_lastname: data.lastName,
|
|
P_MIDDLEINITIAL: data.middleInitial ?? '',
|
|
p_title: data.title,
|
|
p_phoneno: data.phone,
|
|
p_mobileno: data.mobile,
|
|
p_faxno: data.fax,
|
|
p_emailaddress: data.email,
|
|
p_user_id: this.userService.getUser()
|
|
}
|
|
|
|
return this.http.post(`${this.apiUrl}/${this.apiDb}/InsertSPContacts`, contact);
|
|
}
|
|
|
|
updateContact(spContactId: number, data: Contact): Observable<any> {
|
|
|
|
const contact = {
|
|
p_spcontactid: spContactId,
|
|
p_firstname: data.firstName,
|
|
p_lastname: data.lastName,
|
|
P_MIDDLEINITIAL: data.middleInitial ?? '',
|
|
p_title: data.title,
|
|
p_phoneno: data.phone,
|
|
p_mobileno: data.mobile,
|
|
p_faxno: data.fax,
|
|
p_emailaddress: data.email,
|
|
p_user_id: this.userService.getUser()
|
|
}
|
|
|
|
return this.http.put(`${this.apiUrl}/${this.apiDb}/UpdateSPContacts`, contact);
|
|
}
|
|
|
|
deleteContact(spContactId: string): Observable<any> {
|
|
return this.http.patch(`${this.apiUrl}/${this.apiDb}/InactivateSPContact/${spContactId}`, null);
|
|
}
|
|
|
|
createContactLogin(data: ContactLogin): Observable<any> {
|
|
const contact = {
|
|
P_SPID: data.spid,
|
|
P_EMAILADDR: data.emailAddress,
|
|
P_ENABLEPASSWORDPOLICY: 'Y',
|
|
P_DOMAIN: 'test',
|
|
P_LOOKUPCODE: 'test',
|
|
P_USERID: this.userService.getUser()
|
|
}
|
|
|
|
return this.http.post(`${this.apiUrl}/${this.apiDb}/CreateSPLogins`, contact);
|
|
}
|
|
|
|
}
|