Skip to content

Commit

Permalink
Retrieve multi-page data from CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-korin-bcps committed Nov 5, 2024
1 parent 8f34d9f commit 1b5ee1b
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions backend/src/external_api/css/css.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class CssService implements ExternalApiService {
readonly clientSecret: string;
readonly grantType: string;
readonly env: string;
readonly maxPages: number;

@Inject(ConfigurationService)
readonly configService: ConfigurationService;
Expand All @@ -25,6 +26,7 @@ export class CssService implements ExternalApiService {
this.clientSecret = process.env.CSS_CLIENT_SECRET;
this.grantType = "client_credentials";
this.env = process.env.ENVIRONMENT;
this.maxPages = 5;
}

authenticate = async (): Promise<string> => {
Expand Down Expand Up @@ -116,7 +118,7 @@ export class CssService implements ExternalApiService {
}
};

getUserRoleMapping = async (): Promise<AxiosResponse> => {
getUserRoleMapping = async (): Promise<AxiosResponse> => {
try {
const apiToken = await this.authenticate();
//Get all roles from NatCom CSS integation
Expand All @@ -135,26 +137,33 @@ getUserRoleMapping = async (): Promise<AxiosResponse> => {

//Get all users for each role
let usersUrl: string = "";
const pages = Array.from(Array(this.maxPages), (_, i) => i + 1);
const usersRoles = await Promise.all(
roleList.map(async (role) => {
usersUrl = `${this.baseUri}/api/v1/integrations/4794/${this.env}/roles/${role.name}/users`;
const userRes = await get(apiToken, encodeURI(usersUrl), config);
if (userRes?.data.data.length > 0) {
const {
data: { data: users },
} = userRes;
let usersRolesTemp = await Promise.all(
users.map((user) => {
return {
userId: user.username
.replace(/@idir$/i, "")
.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, "$1-$2-$3-$4-$5"),
role: role.name,
};
}),
);
return usersRolesTemp;
let usersRolesTemp = [];
for (const page of pages) {
usersUrl = `${this.baseUri}/api/v1/integrations/4794/${this.env}/roles/${role.name}/users?page=${page}`;
const userRes = await get(apiToken, encodeURI(usersUrl), config);
if (userRes?.data.data.length > 0) {
const {
data: { data: users },
} = userRes;
let usersRolesSinglePage = await Promise.all(
users.map((user) => {
return {
userId: user.username
.replace(/@idir$/i, "")
.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, "$1-$2-$3-$4-$5"),
role: role.name,
};
}),
);
usersRolesTemp.push(...usersRolesSinglePage);
} else {
break;
}
}
return usersRolesTemp;
}),
);

Expand Down

0 comments on commit 1b5ee1b

Please sign in to comment.