Skip to content

Commit

Permalink
feat(api): add perform async job on generic upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xav-car committed Dec 16, 2024
1 parent e2de80e commit 93a1d0d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import { CommonCsvLearnerParser } from '../../../infrastructure/serializers/csv/
import { getDataBuffer } from '../../../infrastructure/utils/bufferize/get-data-buffer.js';
import { AggregateImportError, OrganizationLearnerImportFormatNotFoundError } from '../../errors.js';
import { OrganizationImport } from '../../models/OrganizationImport.js';
import { ValidateCommonOrganizationImportFileJob } from '../../models/ValidateCommonOrganizationImportFileJob.js';

const sendOrganizationLearnersFile = async function ({
payload,
userId,
organizationId,
organizationLearnerImportFormatRepository,
validateCommonOrganizationLearnersJobRepository,
organizationImportRepository,
importStorage,
dependencies = { createReadStream, getDataBuffer },
}) {
const organizationImport = OrganizationImport.create({ organizationId, createdBy: userId });
let organizationImport = OrganizationImport.create({ organizationId, createdBy: userId });
let filename;
let encoding;
const errors = [];
Expand All @@ -25,6 +27,10 @@ const sendOrganizationLearnersFile = async function ({
if (organizationLearnerImportFormat === null)
throw new OrganizationLearnerImportFormatNotFoundError(organizationId);

await organizationImportRepository.save(organizationImport);

organizationImport = await organizationImportRepository.getLastByOrganizationId(organizationId);

const readableStreamEncoding = dependencies.createReadStream(payload.path);
const bufferEncoding = await dependencies.getDataBuffer(readableStreamEncoding);

Expand All @@ -36,6 +42,9 @@ const sendOrganizationLearnersFile = async function ({
encoding = parser.getEncoding();

filename = await importStorage.sendFile({ filepath: payload.path });
await validateCommonOrganizationLearnersJobRepository.performAsync(
new ValidateCommonOrganizationImportFileJob({ organizationImportId: organizationImport.id }),
);
} catch (error) {
if (Array.isArray(error)) {
errors.push(...error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { importCommonOrganizationLearnersJobRepository } from '../../infrastruct
import { importOrganizationLearnersJobRepository } from '../../infrastructure/repositories/jobs/import-organization-learners-job-repository.js';
import { importScoCsvOrganizationLearnersJobRepository } from '../../infrastructure/repositories/jobs/import-sco-csv-organization-learners-job-repository.js';
import { importSupOrganizationLearnersJobRepository } from '../../infrastructure/repositories/jobs/import-sup-organization-learners-job-repository.js';
import { validateCommonOrganizationImportFileJobRepository } from '../../infrastructure/repositories/jobs/validate-common-organization-learners-import-file-job-repository.js';
import { validateCsvOrganizationImportFileJobRepository } from '../../infrastructure/repositories/jobs/validate-csv-organization-learners-import-file-job-repository.js';
import { validateOrganizationImportFileJobRepository } from '../../infrastructure/repositories/jobs/validate-organization-learners-import-file-job-repository.js';
import * as organizationImportRepository from '../../infrastructure/repositories/organization-import-repository.js';
Expand Down Expand Up @@ -82,6 +83,7 @@ const dependencies = {
supOrganizationLearnerRepository,
userReconciliationService,
userRepository,
validateCommonOrganizationImportFileJobRepository,
validateCsvOrganizationImportFileJobRepository,
validateOrganizationImportFileJobRepository,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { AggregateImportError } from '../../../../../../../src/prescription/learner-management/domain/errors.js';
import { OrganizationImport } from '../../../../../../../src/prescription/learner-management/domain/models/OrganizationImport.js';
import { ValidateCommonOrganizationImportFileJob } from '../../../../../../../src/prescription/learner-management/domain/models/ValidateCommonOrganizationImportFileJob.js';
import { sendOrganizationLearnersFile } from '../../../../../../../src/prescription/learner-management/domain/usecases/import-from-feature/send-organization-learners-file.js';
import { CommonCsvLearnerParser } from '../../../../../../../src/prescription/learner-management/infrastructure/serializers/csv/common-csv-learner-parser.js';
import { catchErr, expect, sinon } from '../../../../../../test-helper.js';

describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
let organizationImportRepositoryStub,
organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepositoryStub,
organizationLearnerRepositoryStub,
commonCsvLearnerParserStub,
dependencieStub,
Expand All @@ -16,10 +18,12 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
dataBuffer,
fileEncoding,
organizationId,
organizationImportId,
payload,
dataStream,
s3Filepath,
uploadedFilepath,
organizationImportSavedStub,
userId;

beforeEach(function () {
Expand All @@ -31,22 +35,38 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
dataBuffer = Symbol('DataBuffer');
dataStream = Symbol('DataStream');
importFormat = Symbol('importFormat ');
organizationImportId = Symbol('organizationImportdId');
payload = { path: uploadedFilepath };

importStorageStub = {
sendFile: sinon.stub(),
deleteFile: sinon.stub(),
};

validateCommonOrganizationLearnersJobRepositoryStub = {
performAsync: sinon.stub(),
};

validateCommonOrganizationLearnersJobRepositoryStub.performAsync
.withArgs(new ValidateCommonOrganizationImportFileJob({ organizationImportId }))
.resolves();

dependencieStub = {
createReadStream: sinon.stub(),
getDataBuffer: sinon.stub(),
};

organizationImportRepositoryStub = {
save: sinon.stub(),
getLastByOrganizationId: sinon.stub(),
};

organizationImportSavedStub = { id: organizationImportId, upload: sinon.stub() };

organizationImportRepositoryStub.getLastByOrganizationId
.withArgs(organizationId)
.resolves(organizationImportSavedStub);

commonCsvLearnerParserStub = {
getEncoding: sinon.stub(),
};
Expand Down Expand Up @@ -91,14 +111,15 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});

// then
expect(
organizationImportRepositoryStub.save.calledOnceWith(organizationImportStub),
'organizationImportRepositoryStub.save',
).to.be.true;
expect(organizationImportRepositoryStub.save.getCall(0)).to.have.been.calledWithExactly(organizationImportStub);
expect(organizationImportRepositoryStub.save.getCall(1)).to.have.been.calledWithExactly(
organizationImportSavedStub,
);
expect(importStorageStub.deleteFile.called, 'importStorageStub.delete').to.be.false;
});
});
Expand All @@ -117,6 +138,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});

Expand Down Expand Up @@ -152,6 +174,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});

Expand Down Expand Up @@ -180,6 +203,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});

Expand Down Expand Up @@ -210,7 +234,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {

importStorageStub.sendFile.withArgs({ filepath: payload.path }).resolves(s3Filepath);

organizationImportRepositoryStub.save.withArgs(organizationImportStub).rejects();
organizationImportRepositoryStub.save.withArgs(organizationImportSavedStub).rejects();

// when
try {
Expand All @@ -222,6 +246,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});
} catch {
Expand All @@ -246,6 +271,7 @@ describe('Unit | UseCase | sendOrganizationLearnersFile', function () {
organizationImportRepository: organizationImportRepositoryStub,
organizationLearnerRepository: organizationLearnerRepositoryStub,
organizationLearnerImportFormatRepository: organizationLearnerImportFormatRepositoryStub,
validateCommonOrganizationLearnersJobRepository: validateCommonOrganizationLearnersJobRepositoryStub,
dependencies: dependencieStub,
});
} catch {
Expand Down

0 comments on commit 93a1d0d

Please sign in to comment.