Commit c55e0c0e authored by tushar5526's avatar tushar5526
Browse files

feat: Implemented search user in RC

parent db2ff718
No related merge requests found
Showing with 114 additions and 2 deletions
+114 -2
......@@ -3,6 +3,7 @@ import { lastValueFrom, map } from 'rxjs';
import * as qs from 'qs';
import { HttpService } from '@nestjs/axios';
import { fetchDataFromAcessToken } from 'util/fetchData';
import { searchEntity, updateEntity } from 'util/entityHelper';
@Injectable()
export class AppService {
......@@ -17,7 +18,7 @@ export class AppService {
username: username,
password: password,
grant_type: 'password',
scope: 'openid address phone email',
scope: 'openid phone email address',
});
const config: any = {
url: process.env.ACCESS_TOKEN_URI,
......@@ -27,6 +28,7 @@ export class AppService {
},
data: data,
};
let instituteData;
try {
const res = await lastValueFrom(
this.httpService
......@@ -34,13 +36,51 @@ export class AppService {
.pipe(map((item) => item.data)),
);
const access_token = res.access_token;
return fetchDataFromAcessToken(
instituteData = await fetchDataFromAcessToken(
access_token,
process.env.INFO_URI_CASA,
this.httpService,
);
// search in RC using institute's username
} catch (e) {
throw new HttpException('Get registered on CASA', HttpStatus.NOT_FOUND);
}
console.log(instituteData);
const searchRes: Array<any> = await searchEntity(
this.httpService,
process.env.BASE_URI_RC + 'Institute/search',
{
filters: {
username: {
eq: 'unik-new',
},
},
limit: 1,
offset: 0,
},
);
console.log(searchRes);
if (searchRes.length) {
{
const updatedData = {
name: instituteData.name,
phoneNumber: instituteData.phone ? instituteData.phone : '123456',
email: instituteData.email,
username: instituteData.preferred_username,
address: instituteData.address.address
? instituteData.address
: 'GHAR',
};
const updateActionRes = await updateEntity(
this.httpService,
updatedData,
process.env.BASE_URI_RC + `Institue/${searchRes[0].osid}`,
);
return updateActionRes;
}
} else {
// TODO: call register user helper
return 'register User';
}
}
}
import { HttpService } from '@nestjs/axios';
import { HttpException, HttpStatus } from '@nestjs/common';
import { lastValueFrom, map } from 'rxjs';
export const createEntity = async (
httpService: HttpService,
entityData: any,
endpointUri: string,
) => {
const res = await lastValueFrom(
httpService
.post(endpointUri, entityData, {
headers: { 'Content-type': 'application/json' },
})
.pipe(map((item) => item.data)),
);
return res;
};
export const searchEntity = async (
httpService: HttpService,
endpointUri: string,
entityFilterData: any,
) => {
try {
const res = await lastValueFrom(
httpService
.post(endpointUri, entityFilterData)
.pipe(map((item) => item.data)),
);
return res;
} catch (e) {
console.log(e);
throw new HttpException('500 Internal', HttpStatus.INTERNAL_SERVER_ERROR);
}
};
export const getEntityByID = async (
httpService: HttpService,
endpointUri: string,
) => {
const res = await lastValueFrom(
httpService
.get(endpointUri, {
headers: { 'Content-Type': 'application/json' },
})
.pipe(map((item) => item.data)),
);
return res;
};
export const updateEntity = async (
httpService: HttpService,
entityData: any,
endpointUri: string,
) => {
console.log(endpointUri, entityData);
try {
const res = await lastValueFrom(
httpService
.put(endpointUri, JSON.stringify(entityData), {
headers: { 'Content-type': 'application/json' },
})
.pipe(map((item) => item.data)),
);
return res;
} catch (e) {
console.log(e);
throw new HttpException(
'500 Internal while Updating Entity',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment