Skip to content

Commit

Permalink
Merge pull request #66 from YoginiTayade/search-student-api
Browse files Browse the repository at this point in the history
Completed Student-search-api
  • Loading branch information
ankush-maherwal authored Sep 12, 2024
2 parents 771d2bc + 2aca4f8 commit ad9ac68
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions src/adapters/hasura/altStudent.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,34 +452,76 @@ export class ALTStudentService {
offset = studentSearchDto.limit * (studentSearchDto.page - 1);
}

// Calculate offset based on the page number and limit
const limit = parseInt(studentSearchDto.limit)
? parseInt(studentSearchDto.limit)
: 10000;

let query = "";
let filterQuery = "";
// Allowed fields
const allowedFilterFields = [
"state",
"block",
"district",
"schoolUdise",
"username",
"schoolName",
"class",
"board",
"grade",
"userId",
"studentId",
];
// Validating filters
const invalidFields = Object.keys(studentSearchDto.filters).filter(
(field) => !allowedFilterFields.includes(field)
);
if (invalidFields.length > 0) {
return new ErrorResponse({
errorCode: "400",
errorMessage: `Invalid filter fields: ${invalidFields.join(", ")}`,
});
}

// Build the filter query based on the provided filters
Object.keys(studentSearchDto.filters).forEach((e) => {
if (studentSearchDto.filters[e] && studentSearchDto.filters[e] != "") {
if (e === "board") {
query += `${e}:{_ilike: "%${studentSearchDto.filters[e]?.ilike}%"}`;
filterQuery += `${e}:{_ilike: "%${studentSearchDto.filters[e]?.ilike}%"}`;
} else if (
e === "grade" &&
parseInt(studentSearchDto.filters["grade"])
) {
query += `user: {GroupMemberships: {Group: {grade: {_eq: "${parseInt(
filterQuery += `user: {GroupMemberships: {Group: {grade: {_eq: "${parseInt(
studentSearchDto.filters["grade"]
)}"}}}}`;
} else if (e === "schoolName") {
filterQuery += `School: {name: {_eq: "${studentSearchDto.filters[e]?.eq}"}}`;
} else if (e === "class") {
filterQuery += `School: {Groups: {name: {_eq: "${studentSearchDto.filters[e]?.eq}"}}}`;
} else if (e === "state") {
filterQuery += `state: {_eq: "${studentSearchDto.filters[e]?.eq}"}`;
} else if (e === "district") {
filterQuery += `district: {_eq: "${studentSearchDto.filters[e]?.eq}"}`;
} else if (e === "block") {
filterQuery += `block: {_eq: "${studentSearchDto.filters[e]?.eq}"}`;
} else if (e === "username") {
filterQuery += `user: { username: {_eq: "${studentSearchDto.filters[e]?.eq}"},status: {_eq: true}}`;
} else if (e !== "grade") {
query += `${e}:{_eq:"${studentSearchDto.filters[e]?.eq}"}`;
filterQuery += `${e}:{_eq:"${studentSearchDto.filters[e]?.eq}"}`;
}
}
});
query += `,user: {status: {_eq: true}}`;
// Ensure status is active if username filter is not applied
if (!studentSearchDto.filters.username) {
filterQuery += `,user: {status: {_eq: true}}`;
}

// Construct the GraphQL query to search for students with filters
const data = {
query: `query SearchStudent($limit:Int, $offset:Int) {
Students(where:{${query}} , limit: $limit, offset: $offset,) {
Students(where:{${filterQuery}} , limit: $limit, offset: $offset,) {
annualIncome
caste
schoolUdise
Expand Down Expand Up @@ -530,7 +572,9 @@ export class ALTStudentService {
offset: offset,
},
};
console.log(data);
console.log(data.query);

// Axios configuration for sending the GraphQL request
const config = {
method: "post",
url: this.baseURL,
Expand All @@ -541,16 +585,19 @@ export class ALTStudentService {
},
data: data,
};
const response = await this.axios(config);

// Send the request and handle errors if any
const response = await this.axios(config);
if (response?.data?.errors) {
return new ErrorResponse({
errorCode: response.data.errors[0].extensions,
errorMessage: response.data.errors[0].message,
});
}

// Process the result and map it into a desired format
let result = response.data.data.Students;

const studentResponse = await this.mappedResponse(result);

return new SuccessResponse({
Expand Down

0 comments on commit ad9ac68

Please sign in to comment.