An error occurred while loading the file. Please try again.
-
root authored6847a403
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import {
fetchDataFromAcessTokenPost,
getAccessTokenFromCreds,
} from 'util/fetchData';
import {
registerEntity,
resetPasswordEntity,
searchEntity,
updateEntity,
} from 'util/entityHelper';
import { HttpException, HttpStatus } from '@nestjs/common';
import * as qs from 'qs';
import { lastValueFrom } from 'rxjs';
@Injectable()
export class AppService {
constructor(private readonly httpService: HttpService) {}
getHello(): string {
return 'Hello World!';
}
async loginInstitue(username: string, password: string) {
let res;
// Try Login Using CASA
try {
const data = qs.stringify({
username: username,
password: password,
grant_type: 'password',
scope: 'openid phone address-casa email',
});
res = await getAccessTokenFromCreds(
data,
process.env.ACCESS_TOKEN_URI_CASA,
this.httpService,
process.env.ENCODED_CLIENTID_CLIENTSECRETS_CASA,
);
} catch (e) {
console.log(e);
throw new HttpException(
'Get registered on CASA or invalid creds',
HttpStatus.NOT_FOUND,
);
}
const access_token = res.access_token;
const instituteData = await fetchDataFromAcessTokenPost(
access_token,
process.env.INFO_URI_CASA,
this.httpService,
);
// search in RC using institute's username
console.log(instituteData);
const searchRes: Array<any> = await searchEntity(
this.httpService,
process.env.BASE_URI_RC + 'Institute/search',
{
filters: {
username: {
eq: instituteData.preferred_username,
},
},
limit: 1,
offset: 0,
},
);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
// Update or register institute
const entityData = {
name: instituteData.name,
phoneNumber: instituteData.phone ? instituteData.phone : '',
email: instituteData.email,
username: instituteData.preferred_username,
address: instituteData['address-casa']
? instituteData['address-casa']
: '',
};
console.log(searchRes);
let osid;
if (searchRes.length) {
updateEntity(
this.httpService,
entityData,
process.env.BASE_URI_RC + `Institute/${searchRes[0].osid}`,
);
osid = searchRes[0].osid;
} else {
await registerEntity(
this.httpService,
entityData,
process.env.BASE_URI_RC + `Institute/invite`,
);
}
await resetPasswordEntity(
this.httpService,
process.env.ADMIN_ACCESS_TOKEN_URL,
process.env.RC_RESET_PASSWORD_BASE_URL,
username,
password,
process.env.ADMIN_USERNAME,
process.env.ADMIN_PASS,
process.env.ADMIN_USER_INFO_URL,
);
try {
const data = qs.stringify({
username: username,
password: password,
grant_type: 'password',
scope: 'openid',
client_id: 'registry-frontend',
});
const rc_res = await getAccessTokenFromCreds(
data,
process.env.ACCESS_TOKEN_URI_RC,
this.httpService,
'',
);
return { ...rc_res, osid };
} catch (e) {
console.log(e.response.data);
throw new HttpException(
"Can't get token from RC keycloak - Institute",
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async loginTutor(username: string, password: string) {
// fetch data from backend wrapper
let tutorId;
try {
let _;
[_, tutorId] = username.split('_');
if (_ !== 'tutor') throw new Error('Username should be of form tutor_id');
} catch (e) {
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
throw new HttpException(
'Invalid username or not registered on CASA',
HttpStatus.NOT_FOUND,
);
}
const res = await lastValueFrom(
this.httpService.get(process.env.TUTOR_DATA_CASA_BASE_URI + tutorId),
);
const tutorData = res.data;
// search in RC using Tutor's username
console.log(tutorData);
const searchRes: Array<any> = await searchEntity(
this.httpService,
process.env.BASE_URI_RC + 'Tutor/search',
{
filters: {
username: {
eq: username,
},
},
limit: 1,
offset: 0,
},
);
// Update or register Tutor
const entityData = {
name: tutorData.TutorName,
phoneNumber: tutorData.ContactDetails ? tutorData.ContactDetails : '',
email: '',
username: username,
tutorKey: tutorData.TutorKey.toString(),
centerKey: tutorData.CenterKey.toString(),
qualifications: tutorData.Qualification,
aadhaarNo: tutorData.aadhaarNo,
};
let osid;
if (searchRes.length) {
// Found and Update Data
updateEntity(
this.httpService,
entityData,
process.env.BASE_URI_RC + `Tutor/${searchRes[0].osid}`,
);
osid = searchRes[0].osid;
} else {
// TODO: redirect to login
throw new HttpException('Entity not registered', HttpStatus.NOT_FOUND);
}
try {
const data = qs.stringify({
username: username,
password: password,
grant_type: 'password',
scope: 'openid',
client_id: 'registry-frontend',
});
const rc_res = await getAccessTokenFromCreds(
data,
process.env.ACCESS_TOKEN_URI_RC,
this.httpService,
'',
);
return { ...rc_res, osid };
} catch (e) {
console.log(e.response.data);
throw new HttpException(
"Can't get token from RC keycloak - Tutor",
HttpStatus.INTERNAL_SERVER_ERROR,
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
);
}
}
async loginStudent(username: string, password: string) {
// fetch data from backend wrapper
let studentId;
try {
let _;
[_, studentId] = username.split('_');
if (_ !== 'student')
throw new Error('Username should be of form student_id');
} catch (e) {
console.log(e.response.data);
throw new HttpException(
'Invalid username, username should be of form <entity>_{id}',
HttpStatus.NOT_FOUND,
);
}
const res = await lastValueFrom(
this.httpService.get(process.env.STUDENT_DATA_CASA_BASE_URI + studentId),
);
const studentData = res.data;
// search in RC using Student's username
console.log(studentData);
const searchRes: Array<any> = await searchEntity(
this.httpService,
process.env.BASE_URI_RC + 'Student/search',
{
filters: {
username: {
eq: username,
},
},
limit: 1,
offset: 0,
},
);
// Update or register Studnet
const entityData = {
name: studentData.StudentName,
courseKey: studentData.CourseKey.toString(),
email: '',
username: username,
StudentKey: studentData.StudentProfileKey.toString(),
centerKey: studentData.CenterKey.toString(),
address: studentData.Address,
rollNo: studentData.RollNo.toString(),
dob: studentData.DateOfBirth,
};
console.log();
let osid;
if (searchRes.length) {
// Update Data
updateEntity(
this.httpService,
entityData,
process.env.BASE_URI_RC + `Student/${searchRes[0].osid}`,
);
osid = searchRes[0].osid;
} else {
// TODO: redirect to login
throw new HttpException('Entity not registered', HttpStatus.NOT_FOUND);
}
try {
const data = qs.stringify({
username: username,