Skip to content

Commit

Permalink
Merge pull request #83 from transform1234/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ankush-maherwal authored Nov 27, 2024
2 parents 107273a + b599040 commit 67e110a
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 47 deletions.
2 changes: 2 additions & 0 deletions src/adapters/hasura/altLessonTracking.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { TermsProgramtoRulesDto } from "src/altProgramAssociation/dto/altTermsPr
import { ALTModuleTrackingDto } from "src/altModuleTracking/dto/altModuleTracking.dto";
// import { HasuraUserService } from "./user.adapter";
import { ALTHasuraUserService } from "src/adapters/hasura/altUser.adapter";
import { ALTProgramAssociationSearch } from "src/altProgramAssociation/dto/searchAltProgramAssociation.dto";

@Injectable()
export class ALTLessonTrackingService {
Expand Down Expand Up @@ -1213,4 +1214,5 @@ export class ALTLessonTrackingService {
data: moduleId,
});
}

}
211 changes: 167 additions & 44 deletions src/adapters/hasura/altProgramAssociation.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: subjectListData,
Expand Down Expand Up @@ -115,7 +115,7 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: TermsProgramtoRulesData,
Expand Down Expand Up @@ -171,7 +171,7 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: programData,
Expand Down Expand Up @@ -302,7 +302,7 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: searchData,
Expand All @@ -327,69 +327,80 @@ export class ALTProgramAssociationService {
});
}


public async getGlaUserContent(
request: any,
altTermsProgramDto: TermsProgramtoRulesDto,
page: any,
limit: any
) {

const decoded: any = jwt_decode(request.headers.authorization);
const altUserId = decoded["https://hasura.io/jwt/claims"]["x-hasura-user-id"];
const altUserId =
decoded["https://hasura.io/jwt/claims"]["x-hasura-user-id"];

console.log("altUserId", altUserId)
console.log("altTermsProgramDto", altTermsProgramDto)
console.log("altUserId", altUserId);
console.log("altTermsProgramDto", altTermsProgramDto);

// get programtoRulesData

const programtoRulesData = await this.termsProgramtoRulesData(altTermsProgramDto, request)
const programtoRulesData = await this.termsProgramtoRulesData(
altTermsProgramDto,
request
);

// get altcoursetrackingdetails
const promises = programtoRulesData.map((item) =>
this.altCourseTrackingDetails(item.contentId, altUserId, request)
);

const results = await Promise.allSettled(promises);
console.log("results", results)

console.log("results", results);

function isFulfilled<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T> {
return result.status === 'fulfilled';
function isFulfilled<T>(
result: PromiseSettledResult<T>
): result is PromiseFulfilledResult<T> {
return result.status === "fulfilled";
}

// Filter for fulfilled results, then map to access the data
const trackingDetails = results
.filter(isFulfilled) // Use the type guard to filter only fulfilled results
.map(result => result.value.data.ContentBrowseTracking) // Now TypeScript knows `value` exists
.map((result) => result.value.data.ContentBrowseTracking) // Now TypeScript knows `value` exists
.flat();

console.log("programtoRulesData", programtoRulesData)
console.log("trackingDetails", trackingDetails)
console.log("programtoRulesData", programtoRulesData);
console.log("trackingDetails", trackingDetails);

const seenContentIds = new Set(trackingDetails.map(item => item.contentId));
const seenContentIds = new Set(
trackingDetails.map((item) => item.contentId)
);

// Filter out seen courses from programtoRulesData
const unseenProgramRules = programtoRulesData.filter(item => !seenContentIds.has(item.contentId));
const unseenProgramRules = programtoRulesData.filter(
(item) => !seenContentIds.has(item.contentId)
);

console.log("unseenProgramRules", unseenProgramRules);

// pagination

let paginatedData = this.paginateData(unseenProgramRules, page, limit);
console.log(paginatedData);

if (paginatedData.length < limit) {
const additionalData = programtoRulesData.filter(
(item) => !seenContentIds.has(item.courseId) && !unseenProgramRules.includes(item)
(item) =>
!seenContentIds.has(item.courseId) &&
!unseenProgramRules.includes(item)
);
const additionalPaginatedData = this.paginateData(
additionalData,
1,
limit - paginatedData.length
);
const additionalPaginatedData = this.paginateData(additionalData, 1, limit - paginatedData.length);
paginatedData = [...paginatedData, ...additionalPaginatedData];
}

console.log("paginatedData with fallback", paginatedData);


console.log("paginatedData with fallback", paginatedData);

return new SuccessResponse({
statusCode: 200,
Expand Down Expand Up @@ -421,7 +432,7 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: TermsProgramtoRulesData,
Expand All @@ -438,16 +449,14 @@ export class ALTProgramAssociationService {

const result = response.data.data.ProgramTermAssoc;

console.log("result", JSON.parse(result[0].rules).prog)
console.log("result", JSON.parse(result[0].rules).prog);

return JSON.parse(result[0].rules).prog
return JSON.parse(result[0].rules).prog;
}

async altCourseTrackingDetails(contentId, altUserId, request) {

console.log("contentId", contentId)
console.log("altUserId", altUserId)

console.log("contentId", contentId);
console.log("altUserId", altUserId);

const ProgressTrackingDetails = {
query: `query GetProgressDetails($contentId: String, $userId: uuid!) {
Expand All @@ -471,32 +480,146 @@ export class ALTProgramAssociationService {
method: "post",
url: process.env.ALTHASURA,
headers: {
"Authorization": request.headers.authorization,
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: ProgressTrackingDetails,
};

try {
const response = await this.axios(configData);
const altcoursetrackingdetails = response.data
console.log("altcoursetrackingdetails", altcoursetrackingdetails)
return altcoursetrackingdetails

const altcoursetrackingdetails = response.data;
console.log("altcoursetrackingdetails", altcoursetrackingdetails);
return altcoursetrackingdetails;
} catch (error) {
throw new HttpException(
'data not found',
error.response?.status || 500,
);
throw new HttpException("data not found", error.response?.status || 500);
}


}

paginateData(data, page = 1, limit = 5) {
const startIndex = (page - 1) * limit;
const endIndex = page * limit;

return data.slice(startIndex, endIndex);
}
public async contentSearch(request, body) {
const programId = body.programId;
const subjectCondition = body.subject
? `subject: {_eq: "${body.subject}"}, `
: "";
console.log(programId);

const requestBody = {
request: {
filters: {
primaryCategory: ["Learning Resource", "Practice Question Set"],
},
query: body.searchQuery,
fields: [
"name",
"mimeType",
"identifier",
"medium",
"board",
"subject",
"resourceType",
"primaryCategory",
"contentType",
"organisation",
],
},
};
const sunbirdUrl = process.env.SUNBIRDURL;
let config = {
method: "post",
url:
sunbirdUrl +
`/api/content/v1/search?orgdetails=orgName,email&licenseDetails=name,description,url`,
data: requestBody,
};

const sunbirdSearch = await this.axios(config);

//get the programData from programTermAssoc
const data = {
query: `query MyQuery {
ProgramTermAssoc(where: {programId: {_eq: "${programId}"}, ${subjectCondition}}) {
programId
rules
subject
medium
grade
board
}
}
`,
};
console.log(data.query);

const config_data = {
method: "post",
url: process.env.ALTHASURA,
headers: {
Authorization: request.headers.authorization,
"Content-Type": "application/json",
},
data: data,
};
const response = await this.axios(config_data);

if (response?.data?.errors) {
return new ErrorResponse({
errorCode: response.data.errors[0].extensions,
errorMessage: response.data.errors[0].message,
});
}
const rulesData = response.data.data.ProgramTermAssoc;

//Parse the rules field in rulesData
rulesData.forEach((rule) => {
rule.rules = JSON.parse(rule.rules);
});
//Extract identifiers from sunbirdSearch
const contentIdentifiers = sunbirdSearch.data.result.content.map(
(item) => item.identifier
);
const questionSetIdentifiers = sunbirdSearch.data.result.QuestionSet.map(
(item) => ({
id: item.identifier,
subject: item.subject[0],
})
);

// Process each rule and match contentId
const responseData = [];
rulesData.forEach((rule) => {
rule.rules.prog.forEach((item) => {
if (contentIdentifiers.includes(item.contentId)) {
const matchingQuestionSet = questionSetIdentifiers.find(
(qSet) => qSet.subject === rule.subject
);

responseData.push({
contentId: item.contentId,
subject: rule.subject,
courseId: item.courseId || null,
contentType: item.contentType,
order: item.order,
allowedAttempts: item.allowedAttempts,
criteria: item.criteria,
lesson_questionset: matchingQuestionSet
? matchingQuestionSet.id
: "",
});
}
});
});

// Create the final response
return new SuccessResponse({
statusCode: 200,
message: "Ok.",
data: responseData,
});
}
}
4 changes: 2 additions & 2 deletions src/adapters/hasura/altStudent.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GroupMembershipDtoById } from "src/groupMembership/dto/groupMembership.
import { HasuraGroupService } from "./group.adapter";

@Injectable()
export class ALTStudentService {
export class ALTStudentService {
constructor(
private groupService: HasuraGroupService,
private userService: ALTHasuraUserService,
Expand Down Expand Up @@ -489,7 +489,7 @@ export class ALTStudentService {
) {
return new ErrorResponse({
errorCode: "400",
errorMessage: "Please provide 'schoolName' when 'class' is specified.",
errorMessage: "Please provide 'udiseCode' when 'class' is specified.",
});
}
let classFilter = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,12 @@ export class ALTProgramAssociationController {
limit
);
}

@Post("/contentSearch")
@ApiBasicAuth("access-token")
public async contentSearch(
@Req() request: Request,
@Body() body: any
){
return this.altProgramAssociationService.contentSearch(request,body);
}
}

0 comments on commit 67e110a

Please sign in to comment.