From dc11b3989289a5307e30859dd7e9a1bc623bd3d5 Mon Sep 17 00:00:00 2001 From: Suraj Date: Wed, 5 Feb 2025 13:07:48 +0530 Subject: [PATCH 1/5] teacher current role --- src/adapters/hasura/altUser.adapter.ts | 85 ++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/adapters/hasura/altUser.adapter.ts b/src/adapters/hasura/altUser.adapter.ts index 2d8532d..af290ea 100644 --- a/src/adapters/hasura/altUser.adapter.ts +++ b/src/adapters/hasura/altUser.adapter.ts @@ -23,7 +23,7 @@ import { ALTUserUpdateDto } from "src/altUser/dto/alt-user-update.dto"; export class ALTHasuraUserService { axios = require("axios"); - constructor(private httpService: HttpService) {} + constructor(private httpService: HttpService) { } public async getUser(userId: string, request: any) { const decoded: any = jwt_decode(request.headers.authorization); @@ -894,9 +894,8 @@ export class ALTHasuraUserService { const [firstName, lastName] = obj.name.split(" "); // Step 1: Extract initials - const initials = `${firstName[0].toLowerCase()}${ - lastName ? lastName[0].toLowerCase() : "" - }`; + const initials = `${firstName[0].toLowerCase()}${lastName ? lastName[0].toLowerCase() : "" + }`; const dob = obj.dateOfBirth .trim() @@ -1151,6 +1150,21 @@ export class ALTHasuraUserService { decoded["https://hasura.io/jwt/claims"]["x-hasura-allowed-roles"]; const username = decoded.preferred_username; + + // Fetch Teacher data with currentRole + if (roles[0] === 'teacher') { + console.log("fetchTeacherUserData") + + const userData = await this.fetchTeacherUserData(username, token, roles); + if (!userData) { + return this.sendErrorResponse(res, 404, "User not found or inactive"); + } + + // Send success response + return this.sendSuccessResponse(res, 200, "Authenticated", userData); + + } + // Fetch user details from GraphQL const userData = await this.fetchUserData(username, token, roles); if (!userData) { @@ -1250,6 +1264,66 @@ export class ALTHasuraUserService { } } + async fetchTeacherUserData(username: string, token: string, roles: string[]) { + console.log("fetchUserData username", username); + const query = { + query: ` + query searchUser($username: String!) { + Users(where: {username: {_eq: $username}, status: {_eq: true}}) { + userId + name + username + email + mobile + gender + dateOfBirth + role + status + createdAt + updatedAt + Teachers { + currentRole + } + GroupMemberships(where: {status: {_eq: true}}) { + Group { + board + medium + grade + groupId + schoolUdise + } + School { + name + udiseCode + } + } + } + } + `, + variables: { username }, + }; + + const config = { + method: "post", + url: process.env.ALTHASURA, + headers: { + Authorization: `Bearer ${token}`, + "x-hasura-role": getUserRole(roles), + "Content-Type": "application/json", + }, + data: query, + }; + + try { + const response = await this.axios(config); + console.log("response.data.data", response.data); + return response.data.data.Users || null; + } catch (error) { + console.error("GraphQL fetch error:", error.message); + return null; + } + } + async getUserPoints(request: any, token: string) { const decoded: any = jwt_decode(token); const userId = decoded["https://hasura.io/jwt/claims"]["x-hasura-user-id"]; @@ -1482,8 +1556,7 @@ export class ALTHasuraUserService { process.env.TELEMETRY_DB_URL || `postgres://${process.env.TELEMETRY_DB_USER}:${encodeURIComponent( process.env.TELEMETRY_DB_PASSWORD - )}@${process.env.TELEMETRY_DB_HOST}:${ - process.env.TELEMETRY_DB_PORT + )}@${process.env.TELEMETRY_DB_HOST}:${process.env.TELEMETRY_DB_PORT }/${process.env.TELEMETRY_DB_NAME}?sslmode=disable`; // Create a connection pool From db964edbd2848a22530198bd80cfb479bcaec6fe Mon Sep 17 00:00:00 2001 From: ankush-maherwal Date: Wed, 5 Feb 2025 13:30:20 +0530 Subject: [PATCH 2/5] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 938cb1b..3ae939b 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,3 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors ## License Nest is [MIT licensed](LICENSE). - -#test -. From 94ff91ef6042e5f09c2efd7ba90676070eae767d Mon Sep 17 00:00:00 2001 From: Suraj Date: Wed, 5 Feb 2025 14:04:47 +0530 Subject: [PATCH 3/5] teacher role --- src/adapters/hasura/altUser.adapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapters/hasura/altUser.adapter.ts b/src/adapters/hasura/altUser.adapter.ts index af290ea..d97b472 100644 --- a/src/adapters/hasura/altUser.adapter.ts +++ b/src/adapters/hasura/altUser.adapter.ts @@ -1265,7 +1265,7 @@ export class ALTHasuraUserService { } async fetchTeacherUserData(username: string, token: string, roles: string[]) { - console.log("fetchUserData username", username); + console.log("fetchTeacherUserData username", username); const query = { query: ` query searchUser($username: String!) { From 174128b08b6c2a156f73421cbac77611579f4df4 Mon Sep 17 00:00:00 2001 From: Suraj Date: Wed, 5 Feb 2025 18:40:21 +0530 Subject: [PATCH 4/5] class by desc order --- .../hasura/altProgramAssociation.adapter.ts | 176 +++++++++++++++++- src/adapters/hasura/altUser.adapter.ts | 2 +- 2 files changed, 168 insertions(+), 10 deletions(-) diff --git a/src/adapters/hasura/altProgramAssociation.adapter.ts b/src/adapters/hasura/altProgramAssociation.adapter.ts index 3092cf2..a3e270d 100644 --- a/src/adapters/hasura/altProgramAssociation.adapter.ts +++ b/src/adapters/hasura/altProgramAssociation.adapter.ts @@ -14,7 +14,7 @@ Injectable(); export class ALTProgramAssociationService { axios = require("axios"); - constructor(private readonly httpService: HttpService) {} + constructor(private readonly httpService: HttpService) { } public async mappedResponse(data: any) { const programResponse = data.map((item: any) => { @@ -1533,7 +1533,7 @@ export class ALTProgramAssociationService { const checkGraphQLQuery = { query: ` query MyQuery($groupId: uuid!, $startDate: timestamptz, $endDate: timestamptz) { - Group(where: { groupId: { _eq: $groupId } }) { + Group(where: { groupId: { _eq: $groupId } }, order_by: { grade: desc }) { groupId type grade @@ -1547,11 +1547,10 @@ export class ALTProgramAssociationService { name userId totalPoints: Points_aggregate( - ${ - startDate && endDate - ? "where: { created_at: { _gte: $startDate, _lte: $endDate } }" - : "" - } + ${startDate && endDate + ? "where: { created_at: { _gte: $startDate, _lte: $endDate } }" + : "" + } ) { aggregate { sum { @@ -1625,7 +1624,7 @@ export class ALTProgramAssociationService { const checkGraphQLQuery = { query: ` query MyQuery($schoolUdise: String!, $startDate: timestamptz, $endDate: timestamptz) { - Group(where: { schoolUdise: { _eq: $schoolUdise } }) { + Group(where: { schoolUdise: { _eq: $schoolUdise } }, order_by: { grade: desc }) { groupId type grade @@ -1827,6 +1826,165 @@ export class ALTProgramAssociationService { } } + // async getPointsByBoard1(request, userId, board, startDate, endDate) { + // console.log("userId", userId) + // console.log("board", board) + // console.log("startDate", startDate) + // console.log("endDate", endDate) + // const checkGraphQLQuery = { + // query: ` + // query MyQuery($board: String!) { + // School(where: {board: {_eq: $board}}) { + // board + // udiseCode + // } + // } + // `, + // variables: { + // board: board + // }, + // }; + // const config_data = { + // method: "post", + // url: process.env.ALTHASURA, + // headers: { + // Authorization: request.headers.authorization, + // "Content-Type": "application/json", + // }, + // data: checkGraphQLQuery, + // }; + // try { + // const checkResponse = await this.axios(config_data); + // console.log("checkResponse", checkResponse.data); + // const udiseCodes = checkResponse.data.data?.School.map(item => item.udiseCode); + // console.log("udiseCodes", udiseCodes); + // // const cleanedUdiseCodes = udiseCodes.filter(code => /^[0-9]+$/.test(code)); + // // console.log("cleanedUdiseCodes", cleanedUdiseCodes); + // //return udiseCodes + // // Group + // const checkGraphQLQuery2 = { + // query: ` + // query MyQuery($udiseCodes: [String!]!) { + // Group(where: {schoolUdise: {_in: $udiseCodes}}) { + // schoolUdise + // groupId + // } + // } + // `, + // variables: { + // udiseCodes: udiseCodes + // }, + // }; + // console.log("checkGraphQLQuery2", checkGraphQLQuery2) + + // const config_data2 = { + // method: "post", + // url: process.env.ALTHASURA, + // headers: { + // Authorization: request.headers.authorization, + // "Content-Type": "application/json", + // }, + // data: checkGraphQLQuery2, + // }; + // const checkResponse2 = await this.axios(config_data2); + // console.log("checkResponse2", checkResponse2.data); + // //return checkResponse2.data.data.Group + // const groupids = checkResponse2.data.data.Group.map(item => item.groupId); + // //return groupids; + // // Group + // const checkGraphQLQuery3 = { + // query: ` + // query MyQuery($groupids: [uuid!]!) { + // GroupMembership(where: {groupId: {_in: $groupids}}) { + // groupId + // userId + // } + // } + // `, + // variables: { + // groupids: groupids + // }, + // }; + // console.log("checkGraphQLQuery3", checkGraphQLQuery3) + + // const config_data3 = { + // method: "post", + // url: process.env.ALTHASURA, + // headers: { + // Authorization: request.headers.authorization, + // "Content-Type": "application/json", + // }, + // data: checkGraphQLQuery3, + // }; + // const checkResponse3 = await this.axios(config_data3); + // console.log("checkResponse3", checkResponse3.data); + // //return checkResponse3.data.data + // const userIds = checkResponse3.data.data.GroupMembership.map(item => item.groupId); + // console.log("userIds", userIds) + // //return userIds; + + // // UserPoints + + // const responses = []; + + // for (const userId of userIds) { + // const checkGraphQLQuery4 = { + // query: ` + // query MyQuery($userId: uuid!) { + // UserPoints(where: {user_id: {_eq: $userId}}) { + // user_id + // points + // } + // } + // `, + // variables: { + // userId: userId, + // }, + // }; + + // const config_data4 = { + // method: "post", + // url: process.env.ALTHASURA, + // headers: { + // Authorization: request.headers.authorization, + // "Content-Type": "application/json", + // }, + // data: checkGraphQLQuery4, + // }; + + // try { + // const checkResponse4 = await this.axios(config_data4); + // console.log(`Response for User ID ${userId}:`, checkResponse4.data.data); + + // // Push the response to the array + // responses.push({ + // userId: userId, + // data: checkResponse4.data.data.UserPoints, // Assuming UserPoints holds the relevant data + // }); + // } catch (error) { + // console.error(`Error for User ID ${userId}:`, error.response?.data || error.message); + + // // Optionally, push error info if needed + // responses.push({ + // userId: userId, + // error: error.response?.data || error.message, + // }); + // } + + + // } + + + // } catch (error) { + // console.error("Axios Error:", error.message); + // throw new ErrorResponse({ + // errorCode: "AXIOS_ERROR", + // errorMessage: "Failed to execute the GraphQL mutation.", + // }); + // } + // } + + async getDateRange( timeframes: string ): Promise<{ startDate: string; endDate: string }> { @@ -2687,5 +2845,5 @@ export class ALTProgramAssociationService { data: insertResponse.data.data, }); } - + } diff --git a/src/adapters/hasura/altUser.adapter.ts b/src/adapters/hasura/altUser.adapter.ts index d97b472..6f26ac1 100644 --- a/src/adapters/hasura/altUser.adapter.ts +++ b/src/adapters/hasura/altUser.adapter.ts @@ -1284,7 +1284,7 @@ export class ALTHasuraUserService { Teachers { currentRole } - GroupMemberships(where: {status: {_eq: true}}) { + GroupMemberships(where: {status: {_eq: true}}, order_by: {Group: {grade: desc}}) { Group { board medium From 932d2f6eb71e80f46722caef975bc8c5505b54d6 Mon Sep 17 00:00:00 2001 From: Suraj Date: Thu, 6 Feb 2025 09:23:52 +0530 Subject: [PATCH 5/5] leaderboard points by board --- .../hasura/altProgramAssociation.adapter.ts | 125 ++++++++++++++++-- 1 file changed, 116 insertions(+), 9 deletions(-) diff --git a/src/adapters/hasura/altProgramAssociation.adapter.ts b/src/adapters/hasura/altProgramAssociation.adapter.ts index a3e270d..6c00a83 100644 --- a/src/adapters/hasura/altProgramAssociation.adapter.ts +++ b/src/adapters/hasura/altProgramAssociation.adapter.ts @@ -1726,7 +1726,8 @@ export class ALTProgramAssociationService { console.log("endDate", endDate) const checkGraphQLQuery = { query: ` - query MyQuery($board: String!, $startDate: timestamptz, $endDate: timestamptz) { + query MyQuery($board: String!, $startDate: timestamptz, $endDate: timestamptz) + @cached(ttl: 300, refresh: true) { School(where: { board: { _ilike: $board } }) { board udiseCode @@ -1762,13 +1763,11 @@ export class ALTProgramAssociationService { } } `, - variables: { - board: board, - startDate, - endDate, - }, + variables: { board, startDate, endDate }, }; + + const config_data = { method: "post", url: process.env.ALTHASURA, @@ -1833,6 +1832,115 @@ export class ALTProgramAssociationService { // console.log("endDate", endDate) // const checkGraphQLQuery = { // query: ` + // query MyQuery($board: String!, $startDate: timestamptz, $endDate: timestamptz) { + // School(where: { board: { _ilike: $board } }) { + // board + // udiseCode + // Groups { + // schoolUdise + // groupId + // type + // grade + // name + // topUsers: GroupMemberships( + // order_by: { User: { Points_aggregate: { sum: { points: asc } } } } + // ) { + // groupId + // User { + // name + // userId + // totalPoints: Points_aggregate(where: { created_at: { _gte: $startDate, _lte: $endDate } }) { + // aggregate { + // sum { + // points + // } + // } + // } + // Points(order_by: {created_at: desc}, limit: 1) { + // points + // created_at + // description + // identifier + // } + // } + // } + // } + // } + // } + // `, + // variables: { + // board: board, + // startDate, + // endDate, + // }, + // }; + + + + // const config_data = { + // method: "post", + // url: process.env.ALTHASURA, + // headers: { + // Authorization: request.headers.authorization, + // "Content-Type": "application/json", + // }, + // data: checkGraphQLQuery, + // }; + + // try { + // const checkResponse = await this.axios(config_data); + // console.log("checkResponse", checkResponse.data); + + // if (checkResponse?.data?.errors) { + // return new SuccessResponse({ + // statusCode: 401, + // message: checkResponse?.data?.errors, + // data: checkResponse?.data?.data, + // }); + // } else if (checkResponse?.data?.data) { + // if ( + // !checkResponse.data.data?.School || + // checkResponse.data.data.School.length === 0 + // ) { + // return new SuccessResponse({ + // statusCode: 204, + // message: `No data found for board: ${board}`, + // }); + // } + + // const formattedData = this.transformBoardData( + // checkResponse?.data?.data, + // userId + // ); + + // return new SuccessResponse({ + // statusCode: 200, + // message: "User Points fetched successfully.", + // data: formattedData, + // }); + // } else { + // return new SuccessResponse({ + // statusCode: 200, + // message: "User Points not exists.", + // data: [], + // }); + // } + // } catch (error) { + // console.error("Axios Error:", error.message); + // throw new ErrorResponse({ + // errorCode: "AXIOS_ERROR", + // errorMessage: "Failed to execute the GraphQL mutation.", + // }); + // } + // } + + // async getPointsByBoard2(request, userId, board, startDate, endDate) { + // console.log("userId", userId) + // console.log("board", board) + // console.log("startDate", startDate) + // console.log("endDate", endDate) + // const checkGraphQLQuery = { + // query: ` // query MyQuery($board: String!) { // School(where: {board: {_eq: $board}}) { // board @@ -1858,8 +1966,7 @@ export class ALTProgramAssociationService { // console.log("checkResponse", checkResponse.data); // const udiseCodes = checkResponse.data.data?.School.map(item => item.udiseCode); // console.log("udiseCodes", udiseCodes); - // // const cleanedUdiseCodes = udiseCodes.filter(code => /^[0-9]+$/.test(code)); - // // console.log("cleanedUdiseCodes", cleanedUdiseCodes); + // //return udiseCodes // // Group // const checkGraphQLQuery2 = { @@ -1921,7 +2028,7 @@ export class ALTProgramAssociationService { // //return checkResponse3.data.data // const userIds = checkResponse3.data.data.GroupMembership.map(item => item.groupId); // console.log("userIds", userIds) - // //return userIds; + // return userIds; // // UserPoints