Added esslDeleteEmployee
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 4m13s
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 4m13s
This commit is contained in:
parent
bb3e966daf
commit
72b6bb3cd6
@ -9,7 +9,7 @@ import { DOMParser } from 'xmldom';
|
||||
const logger = getLogger();
|
||||
const corsHandler = getCorsHandler();
|
||||
|
||||
export interface GetEmployeeDetailsRquest {
|
||||
export interface EmployeeCodeRequest {
|
||||
employeeCode: string;
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ const escapeXml = (str: string) => {
|
||||
.replace(/'/g, ''');
|
||||
};
|
||||
|
||||
function createGetEmployeeDetailsRequest(username: string, password: string, employeeCode: string) {
|
||||
function createGetEmployeeDetailsRequest(username: string, password: string, employeeCode: string): string | null {
|
||||
const soapRequest = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
||||
<soap12:Body>
|
||||
@ -124,7 +124,7 @@ function isValidDateString(dateString: string): boolean {
|
||||
return dateRegex.test(dateString);
|
||||
}
|
||||
|
||||
function createUpdateEmployeeExRequest(username: string, password: string, request: UpdateEmployeeExRequest) {
|
||||
function createUpdateEmployeeExRequest(username: string, password: string, request: UpdateEmployeeExRequest): string | null {
|
||||
|
||||
if (!username || !password || !request.employeeCode || !request.employeeName || !request.employeeLocation) {
|
||||
throw new Error('Missing required fields');
|
||||
@ -165,6 +165,31 @@ function parseUpdateEmployeeExResponse(soapResponse: string): string | null {
|
||||
return resultText;
|
||||
}
|
||||
|
||||
function createDeleteEmployeeRequest(username: string, password: string, employeeCode: string): string | null {
|
||||
if (!username || !password || !employeeCode) {
|
||||
throw new Error('Missing required fields');
|
||||
}
|
||||
const soapRequst = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
||||
<soap12:Body>
|
||||
<DeleteEmployee xmlns="http://tempuri.org/">
|
||||
<UserName>${escapeXml(username)}</UserName>
|
||||
<Password>${escapeXml(password)}</Password>
|
||||
<EmployeeCode>${escapeXml(employeeCode)}</EmployeeCode>
|
||||
</DeleteEmployee>
|
||||
</soap12:Body>
|
||||
</soap12:Envelope>`;
|
||||
return soapRequst;
|
||||
}
|
||||
|
||||
function parseDeleteEmployeeResponse(soapResponse: string): string | null {
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(soapResponse, "text/xml");
|
||||
const currentElement = xmlDoc.documentElement.firstChild as HTMLElement;
|
||||
const resultText = currentElement.textContent;
|
||||
return resultText;
|
||||
}
|
||||
|
||||
async function sendSoapRequest(soapRequest: string, endpoint: string) {
|
||||
try {
|
||||
const headers: any = {
|
||||
@ -192,7 +217,7 @@ async function getUserDetails(username: string,
|
||||
password: string,
|
||||
employeeCode: string, endpoint: string) {
|
||||
const soapRequest = createGetEmployeeDetailsRequest(username, password, employeeCode);
|
||||
const soapResponse = await sendSoapRequest(soapRequest, endpoint);
|
||||
const soapResponse = await sendSoapRequest(soapRequest!, endpoint);
|
||||
const parsedResponse = parseGetEmployeeDetailsResponse(soapResponse);
|
||||
return parsedResponse;
|
||||
}
|
||||
@ -202,11 +227,20 @@ async function updateEmployeeEx(username: string,
|
||||
request: UpdateEmployeeExRequest,
|
||||
endpoint: string) {
|
||||
const soapRequest = createUpdateEmployeeExRequest(username, password, request);
|
||||
const soapResponse = await sendSoapRequest(soapRequest, endpoint);
|
||||
const soapResponse = await sendSoapRequest(soapRequest!, endpoint);
|
||||
const parsedResponse = parseUpdateEmployeeExResponse(soapResponse);
|
||||
return parsedResponse;
|
||||
}
|
||||
|
||||
async function deleteEmplyee(username: string,
|
||||
password: string,
|
||||
employeeCode: string, endpoint: string) {
|
||||
const soapRequest = createDeleteEmployeeRequest(username, password, employeeCode);
|
||||
const soapResponse = await sendSoapRequest(soapRequest!, endpoint);
|
||||
const parsedResponse = parseDeleteEmployeeResponse(soapResponse);
|
||||
return parsedResponse;
|
||||
}
|
||||
|
||||
export const esslGetUserDetails = onRequest({
|
||||
region: '#{SERVICES_RGN}#'
|
||||
}, async (request: Request, response: Response) => {
|
||||
@ -217,7 +251,7 @@ export const esslGetUserDetails = onRequest({
|
||||
let endpoint: string | null = request.body.endpoint as string;
|
||||
let gymId: string | null = request.body.gymId as string;
|
||||
|
||||
const getEmployeeDetailsRequest = request.body.params as GetEmployeeDetailsRquest;
|
||||
const getEmployeeDetailsRequest = request.body.params as EmployeeCodeRequest;
|
||||
|
||||
if (!username) {
|
||||
throw new Error('Missing username or password');
|
||||
@ -313,3 +347,54 @@ export const esslUpdateUser = onRequest({
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export const esslDeleteEmployee = onRequest({
|
||||
region: '#{SERVICES_RGN}#'
|
||||
}, async (request: Request, response: Response) => {
|
||||
return corsHandler(request, response, async () => {
|
||||
let username: string | null = request.body.username as string;
|
||||
let password: string | null = request.body.password as string;
|
||||
let endpoint: string | null = request.body.endpoint as string;
|
||||
let gymId: string | null = request.body.gymId as string;
|
||||
|
||||
const getEmployeeDetailsRequest = request.body.params as EmployeeCodeRequest;
|
||||
|
||||
if (!username) {
|
||||
throw new Error('Missing username or password');
|
||||
}
|
||||
username = username.trim();
|
||||
if (!password) {
|
||||
if (!gymId) {
|
||||
throw new Error('Missing password or gymId');
|
||||
}
|
||||
// todo: Get password from gym configuration by decrypting with private key
|
||||
}
|
||||
password = password.trim();
|
||||
if (!getEmployeeDetailsRequest) {
|
||||
throw new Error('Missing request params');
|
||||
}
|
||||
const employeeCode = getEmployeeDetailsRequest.employeeCode;
|
||||
if (!employeeCode) {
|
||||
throw new Error('Missing employeeCode');
|
||||
}
|
||||
if (!endpoint) {
|
||||
throw new Error('Missing endpoint');
|
||||
}
|
||||
if (!endpoint || endpoint.trim() === '') {
|
||||
throw new Error('Missing endpoint');
|
||||
}
|
||||
try {
|
||||
new URL(endpoint);
|
||||
} catch (_) {
|
||||
throw new Error('Endpoint is not a valid URI or URL');
|
||||
}
|
||||
if (!endpoint.endsWith('/webservice.asmx')) {
|
||||
if (endpoint.endsWith('/')) {
|
||||
endpoint = endpoint.substring(0, endpoint.length - 1);
|
||||
}
|
||||
endpoint += '/webservice.asmx';
|
||||
}
|
||||
const result = await deleteEmplyee(username, password, employeeCode, endpoint);
|
||||
response.send(result);
|
||||
});
|
||||
});
|
||||
|
||||
@ -1 +1 @@
|
||||
export { esslGetUserDetails, esslUpdateUser } from './essl';
|
||||
export { esslGetUserDetails, esslUpdateUser, esslDeleteEmployee } from './essl';
|
||||
@ -17,4 +17,4 @@ export { processNotificationOnCreate } from './notifications';
|
||||
export * from './payments';
|
||||
export { getPlaceDetails, getPlacesAutocomplete } from './places';
|
||||
export { registerClient } from './users';
|
||||
export { esslGetUserDetails, esslUpdateUser } from './dooraccess';
|
||||
export { esslGetUserDetails, esslUpdateUser, esslDeleteEmployee } from './dooraccess';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user