-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbsSelectService.ts
More file actions
148 lines (129 loc) · 4.82 KB
/
bsSelectService.ts
File metadata and controls
148 lines (129 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { APIRequestContext, APIResponse } from '@playwright/test';
import * as apiClient from '../apiClient';
import { config } from '../../config/env';
import { ApiResponse, QueryParams } from '../core/types';
import { ParticipantRecord } from '../../interface/InputData';
export const getRecordsFromBsSelectRetrieveCohort = (
request: APIRequestContext,
params: QueryParams
): Promise<ApiResponse> => {
const promise = apiClient.get(request, `${config.endpointBsSelectRetrieveCohortDistributionData}${config.routeBsSelectRetrieveCohortDistributionData}`, params);
return new Promise<ApiResponse>((resolve, reject) => {
promise.then(
result => setTimeout(() => resolve(result), config.apiWaitTime),
error => setTimeout(() => reject(new Error(typeof error === 'string' ? error : JSON.stringify(error))), config.apiWaitTime)
);
});
};
export const getRecordsFromBsSelectRetrieveAudit = (
request: APIRequestContext,
params?: QueryParams
): Promise<ApiResponse> => {
return apiClient.get(request, `${config.endpointBsSelectRetrieveCohortRequestAudit}${config.routeBsSelectRetrieveCohortRequestAudit}`, params);
};
export const getRecordsFromParticipantManagementService = (
request: APIRequestContext
): Promise<ApiResponse> => {
return apiClient.get(request, `${config.endpointParticipantManagementDataService}api/${config.participantManagementService}`);
};
export const getRecordsFromParticipantDemographicService = (
request: APIRequestContext
): Promise<ApiResponse> => {
return apiClient.get(request, `${config.endpointParticipantDemographicDataService}api/${config.participantDemographicDataService}`);
};
export const getRecordsFromExceptionManagementService = (
request: APIRequestContext
): Promise<ApiResponse> => {
return apiClient.get(request, `${config.endpointExceptionManagementDataService}api/${config.exceptionManagementService}`);
};
export const getRecordsFromNemsSubscription = (
request: APIRequestContext,
nhsNumbers: string
): Promise<ApiResponse> => {
return apiClient.get(request, `${config.SubToNems}${config.CheckNemsSubPath}?nhsNumber=${nhsNumbers}`);
};
export function extractSubscriptionID(response: ApiResponse): string | null {
const source =
(typeof response.text === 'string' && response.text.length > 0)
? response.text
: JSON.stringify(response.data ?? '');
const cleaned = source.replace(/[\r\n\t]+/g, ' ').replace(/\s+/g, ' ').trim();
const match = cleaned.match(/Subscription ID:\s*([a-f0-9]{32})/i);
return match ? match[1] : null;
}
export const deleteParticipant = (
request: APIRequestContext,
payload: {
NhsNumber: string;
FamilyName: string;
DateOfBirth: string;
}
): Promise<ApiResponse> => {
const endpoint = `${config.endpointBsSelectDeleteParticipant}${config.routeBsSelectDeleteParticipant}`;
return apiClient.post(request, endpoint, payload);
};
export const BlockParticipant = (
request: APIRequestContext,
payload: {
NhsNumber: string;
FamilyName: string;
DateOfBirth: string;
}
): Promise<ApiResponse> => {
const endpoint = `${config.endpointBsSelectUpdateBlockFlag}${config.routeBsSelectBlockParticipant}`;
return apiClient.postWithQuery(request, endpoint, payload);
};
export const UnblockParticipant = (
request: APIRequestContext,
payload: {
NhsNumber: string;
FamilyName: string;
DateOfBirth: string;
}
): Promise<ApiResponse> => {
const endpoint = `${config.endpointBsSelectUpdateBlockFlag}${config.routeBsSelectUnblockParticipant}`;
return apiClient.postWithQuery(request, endpoint, payload);
};
export const receiveParticipantViaServiceNow = (
request: APIRequestContext,
payload: ParticipantRecord
): Promise<ApiResponse> => {
const endpoint = `${config.endpointSerNowReceiveParticipant}${config.routeSerNowReceiveParticipant}`;
return apiClient.post(request, endpoint, payload);
};
export const invalidServiceNowEndpoint = (
request: APIRequestContext,
payload: ParticipantRecord
): Promise<ApiResponse> => {
const endpoint = `${config.invalidEndpointSerNow}${config.invalidRouteSerNowEndpoint}`;
return apiClient.post(request, endpoint, payload);
};
export async function retry<T>(
fn: () => Promise<T>,
validate: (result: T) => boolean,
options?: {
retries?: number;
delayMs?: number;
throwLastError?: boolean;
}
): Promise<T> {
const { retries = 5, delayMs = 2000, throwLastError = true } = options || {};
let lastError: any;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const result = await fn();
if (validate(result)) {
return result;
}
} catch (err) {
lastError = err;
}
if (attempt < retries) {
await new Promise(res => setTimeout(res, delayMs));
}
}
if (throwLastError && lastError) {
throw lastError;
}
throw new Error(`Retry validation failed after ${retries} attempts`);
}