Skip to content

Commit

Permalink
refactor(api): handle providedDate in job data
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Feb 12, 2025
1 parent 6a3c699 commit 73e7428
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.j
import * as organizationLearnerRepository from '../../../../shared/infrastructure/repositories/organization-learner-repository.js';
import { logger } from '../../../../shared/infrastructure/utils/logger.js';
import { computeCertificabilityJobRepository } from '../../../learner-management/infrastructure/repositories/jobs/compute-certificability-job-repository.js';
import { ComputeOrganizationLearnerCertificabilityJobProvidedDateError } from '../../domain/errors.js';
import { usecases } from '../../domain/usecases/index.js';

class ScheduleComputeOrganizationLearnersCertificabilityJobController extends JobScheduleController {
Expand All @@ -27,6 +28,8 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo
}) {
const useProvidedDateRange = data?.useProvidedDateRange;
const onlyNotComputed = data?.onlyNotComputed;
const providedDateRange = data?.providedDateRange;

const {
chunkSize,
cron: cronConfig,
Expand All @@ -35,10 +38,20 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo

const isolationLevel = 'repeatable read';

const parsedCron = cronParser.parseExpression(cronConfig, { tz: 'Europe/Paris' });
const toUserActivityDate = parsedCron.prev().toDate();

const fromUserActivityDate = dayjs(toUserActivityDate).subtract(1, 'day').toDate();
let toUserActivityDate, fromUserActivityDate;
if (providedDateRange && providedDateRange.startDate && providedDateRange.endDate) {
const parseStartDate = dayjs(providedDateRange.startDate, 'YYYY-MM-DD');
const parseEndDate = dayjs(providedDateRange.endDate, 'YYYY-MM-DD');
if (!parseStartDate.isValid() || (!parseEndDate.isValid() && parseStartDate.isBefore(parseEndDate))) {
throw new ComputeOrganizationLearnerCertificabilityJobProvidedDateError();
}
fromUserActivityDate = parseStartDate.toDate();
toUserActivityDate = parseEndDate.toDate();
} else {
const parsedCron = cronParser.parseExpression(cronConfig, { tz: 'Europe/Paris' });
toUserActivityDate = parsedCron.prev().toDate();
fromUserActivityDate = dayjs(toUserActivityDate).subtract(1, 'day').toDate();
}

return await DomainTransaction.execute(
async () => {
Expand Down
7 changes: 7 additions & 0 deletions api/src/prescription/learner-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ class OrganizationLearnerCertificabilityNotUpdatedError extends DomainError {
}
}

class ComputeOrganizationLearnerCertificabilityJobProvidedDateError extends DomainError {
constructor() {
super('Provided date are not valid', 'COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY_JOB_DATE');
}
}

export {
AggregateImportError,
ComputeOrganizationLearnerCertificabilityJobProvidedDateError,
CouldNotDeleteLearnersError,
OrganizationDoesNotHaveFeatureEnabledError,
OrganizationLearnerCertificabilityNotUpdatedError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ describe('Unit | Infrastructure | Jobs | scheduleComputeOrganizationLearnersCert
// given
const useProvidedDateRange = true;
const onlyNotComputed = true;
const providedDateRange = { startDate: '2024-03-01', endDate: '2024-03-03' };

fromUserActivityDate = dayjs(providedDateRange.startDate).toDate();
toUserActivityDate = dayjs(providedDateRange.endDate).toDate();
organizationLearnerRepository.countByOrganizationsWhichNeedToComputeCertificability
.withArgs({
fromUserActivityDate,
Expand Down Expand Up @@ -173,6 +176,7 @@ describe('Unit | Infrastructure | Jobs | scheduleComputeOrganizationLearnersCert
data: {
useProvidedDateRange,
onlyNotComputed,
providedDateRange,
},
dependencies: {
logger,
Expand Down Expand Up @@ -315,6 +319,10 @@ describe('Unit | Infrastructure | Jobs | scheduleComputeOrganizationLearnersCert
// given
const useProvidedDateRange = true;
const onlyNotComputed = true;
const providedDateRange = { startDate: '2024-03-01', endDate: '2024-03-03' };

fromUserActivityDate = dayjs(providedDateRange.startDate).toDate();
toUserActivityDate = dayjs(providedDateRange.endDate).toDate();

organizationLearnerRepository.countByOrganizationsWhichNeedToComputeCertificability
.withArgs({
Expand Down Expand Up @@ -352,6 +360,7 @@ describe('Unit | Infrastructure | Jobs | scheduleComputeOrganizationLearnersCert
data: {
useProvidedDateRange,
onlyNotComputed,
providedDateRange,
},
dependencies: {
logger,
Expand Down

0 comments on commit 73e7428

Please sign in to comment.