Skip to content

Commit

Permalink
Merge pull request #8 from Xitija/student-user-apis
Browse files Browse the repository at this point in the history
Task #204445: User API refactoring
  • Loading branch information
snehal0904 authored Sep 14, 2023
2 parents 5877da8 + a445b7b commit 5e39f0b
Show file tree
Hide file tree
Showing 15 changed files with 1,031 additions and 39 deletions.
18 changes: 16 additions & 2 deletions src/adapters/hasura/adapter.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ function getUserGroup(role: string) {
}
}

function getUsername(obj: any) {
return (
obj.name.toLowerCase().replace(/ /g, "") +
obj.dateOfBirth.replace(/\-/g, "")
);
}

async function getToken() {
const axios = require("axios");
const qs = require("qs");
Expand Down Expand Up @@ -92,7 +99,8 @@ async function createUserInKeyCloak(query, token) {
try {
userResponse = await axios(config);
} catch (e) {
console.log(e);
console.log(e, "Keycloak Creation error");
return e;
}

const userString = userResponse.headers.location;
Expand All @@ -102,4 +110,10 @@ async function createUserInKeyCloak(query, token) {
return result;
}

export { getUserGroup, getUserRole, getToken, createUserInKeyCloak };
export {
getUserGroup,
getUserRole,
getToken,
createUserInKeyCloak,
getUsername,
};
30 changes: 18 additions & 12 deletions src/adapters/hasura/altStudent.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import jwt_decode from "jwt-decode";
import { SuccessResponse } from "src/success-response";
import { StudentDto } from "src/altStudent/dto/alt-student.dto";
import { ErrorResponse } from "src/error-response";
import { HasuraUserService } from "./user.adapter";
import {
getUserGroup,
getUserRole,
getToken,
createUserInKeyCloak,
} from "./adapter.utils";
import { getUserRole } from "./adapter.utils";
import { ALTHasuraUserService } from "./altUser.adapter";

@Injectable()
export class ALTStudentService {
constructor(
private httpService: HttpService,
private userService: HasuraUserService
private userService: ALTHasuraUserService
) {}

baseURL = process.env.ALTHASURA;
Expand Down Expand Up @@ -103,6 +98,10 @@ export class ALTStudentService {
const creatorUserId =
decoded["https://hasura.io/jwt/claims"]["x-hasura-user-id"];

studentDto.createdBy = creatorUserId;
studentDto.updatedBy = creatorUserId;
studentDto.role = "student";

if (altUserRoles.includes("systemAdmin")) {
const createdUser: any = await this.userService.createUser(
request,
Expand All @@ -111,9 +110,7 @@ export class ALTStudentService {

if (createdUser.statusCode === 200) {
studentDto.userId = createdUser.data.userId;
studentDto.createdBy = creatorUserId;
studentDto.updatedBy = creatorUserId;
const studentSchema = new StudentDto(studentDto,false);
const studentSchema = new StudentDto(studentDto, false);
let query = "";

Object.keys(studentDto).forEach((e) => {
Expand All @@ -139,6 +136,9 @@ export class ALTStudentService {
insert_Students_one(object: {${query}}) {
studentId
userId
user {
username
}
}
}
`,
Expand All @@ -161,6 +161,7 @@ export class ALTStudentService {
const response = await this.axios(config);

if (response?.data?.errors) {
console.log(response.data.errors);
return new ErrorResponse({
errorCode: response.data.errors[0].extensions,
errorMessage: response.data.errors[0].message,
Expand All @@ -174,6 +175,11 @@ export class ALTStudentService {
data: result,
});
}
} else {
return new ErrorResponse({
errorCode: "500",
errorMessage: createdUser?.errorMessage,
});
}
} else {
return new ErrorResponse({
Expand Down Expand Up @@ -215,7 +221,7 @@ export class ALTStudentService {
// createdAt: item?.created ? `${item.created}` : "",
// updatedAt: item?.updated ? `${item.updated}` : "",
};
return new StudentDto(studentMapping,true);
return new StudentDto(studentMapping, true);
});

return studentResponse;
Expand Down
Loading

0 comments on commit 5e39f0b

Please sign in to comment.