-
Notifications
You must be signed in to change notification settings - Fork 23
Issue #000000 Fix: Elasticsearch code improved #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: aspire-leaders
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,13 @@ import { FormSubmissionSearchDto } from 'src/forms/dto/form-submission-search.dt | |
| import { FormsService } from 'src/forms/forms.service'; | ||
| import { isElasticsearchEnabled } from 'src/common/utils/elasticsearch.util'; | ||
| import { UserElasticsearchService } from 'src/elasticsearch/user-elasticsearch.service'; | ||
| import { ElasticsearchDataFetcherService } from 'src/elasticsearch/elasticsearch-data-fetcher.service'; | ||
| import axios from 'axios'; | ||
| import { | ||
| ElasticsearchSyncService, | ||
| SyncSection, | ||
| } from '../../elasticsearch/elasticsearch-sync.service'; | ||
|
|
||
| @Injectable() | ||
| export class PostgresCohortMembersService { | ||
| constructor( | ||
|
|
@@ -56,7 +62,9 @@ export class PostgresCohortMembersService { | |
| private readonly userService: PostgresUserService, | ||
| private readonly formsService: FormsService, | ||
| private readonly formSubmissionService: FormSubmissionService, | ||
| private readonly userElasticsearchService: UserElasticsearchService | ||
| private readonly userElasticsearchService: UserElasticsearchService, | ||
| private readonly elasticsearchDataFetcherService: ElasticsearchDataFetcherService, | ||
| private readonly elasticsearchSyncService: ElasticsearchSyncService | ||
| ) {} | ||
|
|
||
| //Get cohort member | ||
|
|
@@ -720,7 +728,20 @@ export class PostgresCohortMembersService { | |
| // Update Elasticsearch with cohort member status | ||
| if (isElasticsearchEnabled()) { | ||
| try { | ||
| // First get the existing user document from Elasticsearch | ||
| // Use comprehensive sync to get complete user document including courses and assessment data | ||
| const userDocument = | ||
| await this.elasticsearchDataFetcherService.comprehensiveUserSync( | ||
| cohortMembers.userId | ||
| ); | ||
|
|
||
| if (!userDocument) { | ||
| LoggerUtil.warn( | ||
| `User document not found for ${cohortMembers.userId}, skipping Elasticsearch update` | ||
| ); | ||
| return; | ||
| } | ||
|
Comment on lines
+731
to
+742
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Fix early return issue breaking cohort member creation flow The code returns early if the user document is not found, but this return statement doesn't provide a response object, which will break the API endpoint. Additionally, the early return prevents the cohort member from being saved in the database. Apply this fix to handle the missing user document gracefully while allowing the database operation to continue: if (!userDocument) {
LoggerUtil.warn(
`User document not found for ${cohortMembers.userId}, skipping Elasticsearch update`
);
- return;
+ // Continue with the database operation even if Elasticsearch sync fails
} else {
+ // Get the existing user document from Elasticsearch
+ const userDoc = await this.userElasticsearchService.getUser(
+ cohortMembers.userId
+ );
+ // ... rest of the Elasticsearch update logic
}
-
-// Get the existing user document from Elasticsearch
-const userDoc = await this.userElasticsearchService.getUser(
- cohortMembers.userId
-);
🤖 Prompt for AI Agents |
||
|
|
||
| // Get the existing user document from Elasticsearch | ||
| const userDoc = await this.userElasticsearchService.getUser( | ||
| cohortMembers.userId | ||
| ); | ||
|
|
@@ -733,6 +754,7 @@ export class PostgresCohortMembersService { | |
| ? [...source.applications] | ||
| : []; | ||
|
|
||
| // Find existing application for this cohort | ||
| const appIndex = applications.findIndex( | ||
| (app) => app.cohortId === cohortMembers.cohortId | ||
| ); | ||
|
|
@@ -754,14 +776,15 @@ export class PostgresCohortMembersService { | |
| }); | ||
| } | ||
|
|
||
| // Now update the user document in Elasticsearch with the merged applications array | ||
| // Now update the user document in Elasticsearch with comprehensive data | ||
| const baseDoc = | ||
| typeof userDoc?._source === 'object' ? userDoc._source : {}; | ||
| await this.userElasticsearchService.updateUser( | ||
| cohortMembers.userId, | ||
| { doc: { ...baseDoc, applications } }, | ||
| { doc: userDocument }, // Use comprehensive user document | ||
| async (userId: string) => { | ||
| return await this.formSubmissionService.buildUserDocumentForElasticsearch( | ||
| // Use comprehensive sync to build the full user document for Elasticsearch | ||
| return await this.elasticsearchDataFetcherService.comprehensiveUserSync( | ||
| userId | ||
| ); | ||
| } | ||
|
|
@@ -775,6 +798,15 @@ export class PostgresCohortMembersService { | |
| ); | ||
| } | ||
| } | ||
|
|
||
| // Sync to Elasticsearch using centralized service | ||
| if (isElasticsearchEnabled()) { | ||
| await this.elasticsearchSyncService.syncUserToElasticsearch( | ||
| cohortMembers.userId, | ||
| { section: SyncSection.APPLICATIONS } | ||
| ); | ||
| } | ||
|
|
||
| return APIResponse.success( | ||
| res, | ||
| apiId, | ||
|
|
@@ -1013,7 +1045,7 @@ export class PostgresCohortMembersService { | |
| completionPercentageRanges: { min: number; max: number }[], | ||
| formId: string | ||
| ): { query: string; parameters: any[]; limit: number; offset: number } { | ||
| // Build completion percentage filter conditions with proper casting | ||
| // Build completion percentage filter conditions with proper numeric casting | ||
| const completionConditions = completionPercentageRanges | ||
| .map( | ||
| (range) => | ||
|
|
@@ -1176,17 +1208,17 @@ export class PostgresCohortMembersService { | |
| : undefined; | ||
|
|
||
| if (!existingApplication) { | ||
| // If application is missing, build and upsert the full user document (with progress pages) | ||
| // If application is missing, use comprehensive sync to build and upsert the full user document | ||
| const fullUserDoc = | ||
| await this.formSubmissionService.buildUserDocumentForElasticsearch( | ||
| await this.elasticsearchDataFetcherService.comprehensiveUserSync( | ||
| cohortMembershipToUpdate.userId | ||
| ); | ||
| if (fullUserDoc) { | ||
| await this.userElasticsearchService.updateUser( | ||
| cohortMembershipToUpdate.userId, | ||
| { doc: fullUserDoc }, | ||
| async (userId: string) => { | ||
| return await this.formSubmissionService.buildUserDocumentForElasticsearch( | ||
| return await this.elasticsearchDataFetcherService.comprehensiveUserSync( | ||
| userId | ||
| ); | ||
| } | ||
|
|
@@ -1217,6 +1249,14 @@ export class PostgresCohortMembersService { | |
| } | ||
| } | ||
|
|
||
| // Sync to Elasticsearch using centralized service | ||
| if (isElasticsearchEnabled()) { | ||
| await this.elasticsearchSyncService.syncUserToElasticsearch( | ||
| cohortMembershipToUpdate.userId, | ||
| { section: SyncSection.APPLICATIONS } | ||
| ); | ||
| } | ||
|
|
||
| // Send notification if applicable for this status only | ||
| let notifyStatuses: string[] = []; | ||
| const { status, statusReason } = cohortMembersUpdateDto; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ import { RbacModule } from './rbac/rbac.module'; | |||||
| import { AssignTenantModule } from './userTenantMapping/user-tenant-mapping.module'; | ||||||
| import { FormsModule } from './forms/forms.module'; | ||||||
| import { HttpService } from '@utils/http-service'; | ||||||
| import { LMSService } from './common/services/lms.service'; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use double quotes for import path Replace single quotes with double quotes to maintain consistency with the project's style guide. Apply this diff: -import { LMSService } from './common/services/lms.service';
+import { LMSService } from "./common/services/lms.service";📝 Committable suggestion
Suggested change
🧰 Tools🪛 ESLint[error] 24-24: Replace (prettier/prettier) 🤖 Prompt for AI Agents |
||||||
| import { TenantModule } from './tenant/tenant.module'; | ||||||
| import { AcademicyearsModule } from './academicyears/academicyears.module'; | ||||||
| import { CohortAcademicYearModule } from './cohortAcademicYear/cohortAcademicYear.module'; | ||||||
|
|
@@ -84,6 +85,7 @@ import { BulkImportModule } from './bulk-import/bulk-import.module'; | |||||
| providers: [ | ||||||
| AppService, | ||||||
| HttpService, | ||||||
| LMSService, | ||||||
| { | ||||||
| provide: 'STORAGE_CONFIG', | ||||||
| useValue: storageConfig, | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.