Skip to content

Commit

Permalink
feat(api): add productor persistance add producerId to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmeunier committed Oct 19, 2023
1 parent 6b7f47e commit 21453cd
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScdlGrantEntity } from "../@types/ScdlGrantEntity";

export default interface MiscScdlDataEntity extends ScdlGrantEntity {
export default interface MiscScdlGrantEntity extends ScdlGrantEntity {
producerId: string;
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
import dedent from "dedent";
import fs from "fs";
jest.mock("fs");
const mockedFs = jest.mocked(fs);
import ExportDateError from "../../../../../shared/errors/cliErrors/ExportDateError";
import ScdlCliController from "./scdl.cli";
import scdlService from "../../scdl.service";
jest.mock("../../scdl.service");
const mockedScdlService = jest.mocked(scdlService);
import ScdlGrantParser from "../../scdl.grant.parser";
import MiscScdlGrant from "../../__fixtures__/MiscScdlGrant";
import { ObjectId } from "mongodb";
import { NotFoundError } from "../../../../../shared/errors/httpErrors";
jest.mock("../../scdl.grant.parser");
const mockedScdlGrantParser = jest.mocked(ScdlGrantParser);

describe("ScdlCliController", () => {
const PRODUCER_ID = "PRODUCER_ID";
const PRODUCER_NAME = "PRODUCER_NAME";
const GRANT = { ...MiscScdlGrant };
const CSV_CONTENT = "CSV_CONTENT";
const PRODUCER_ENTITY = {
_id: new ObjectId(),
producerId: PRODUCER_ID,
producerName: PRODUCER_NAME,
lastUpdate: new Date(),
};

const FILE_PATH = "FILE_PATH";

let cli: ScdlCliController;
beforeEach(() => {
mockedFs.readFileSync.mockReturnValue(CSV_CONTENT);
mockedScdlService.getProducer.mockResolvedValue(PRODUCER_ENTITY);
mockedScdlGrantParser.parseCsv.mockReturnValue([GRANT]);
cli = new ScdlCliController();
});

describe("_parse()", () => {
describe("addProducer()", () => {
it("should call scdlService.createProducer()", async () => {
await cli.addProducer(PRODUCER_ID, PRODUCER_NAME);
expect(scdlService.createProducer).toHaveBeenCalledWith({
producerId: PRODUCER_ID,
producerName: PRODUCER_NAME,
lastUpdate: expect.any(Date),
});
});
});

describe("parse()", () => {
afterEach(() => {
mockedScdlGrantParser.parseCsv.mockReset();
mockedScdlService.getProducer.mockReset();
});

it("should throw ExportDateError", async () => {
// @ts-expect-error: private method
expect(() => cli._parse()).rejects.toThrowError(ExportDateError);
expect(() => cli.parse(FILE_PATH, PRODUCER_ID)).rejects.toThrowError(ExportDateError);
});

it("should throw NotFoundError when providerId does not match any provider in database", async () => {
mockedScdlService.getProducer.mockResolvedValue(null);
expect(() => cli.parse(FILE_PATH, "WRONG_ID", new Date())).rejects.toThrowError(NotFoundError);
});

it("should call ScdlGrantParser.parseCsv()", async () => {
const EXPORT_DATE = new Date();
// @ts-expect-error: private method
await cli._parse(FILE_PATH, [], EXPORT_DATE);
await cli.parse(FILE_PATH, PRODUCER_ID, EXPORT_DATE);
expect(ScdlGrantParser.parseCsv).toHaveBeenLastCalledWith(CSV_CONTENT);
});

it("should call scdlService.createManyGrants()", async () => {
// @ts-expect-error: private method
await cli._parse(FILE_PATH, [], new Date());
await cli.parse(FILE_PATH, PRODUCER_ID, new Date());
expect(scdlService.createManyGrants).toHaveBeenCalledWith([
{
...GRANT,
Expand Down
22 changes: 12 additions & 10 deletions packages/api/src/modules/providers/scdl/interfaces/cli/scdl.cli.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import fs from "fs";
import CliController from "../../../../../shared/CliController";
import ExportDateError from "../../../../../shared/errors/cliErrors/ExportDateError";
import ScdlGrantParser from "../../scdl.grant.parser";
import scdlService from "../../scdl.service";
import MiscScdlGrantEntity from "../../entities/MiscScdlGrantEntity";
import { NotFoundError } from "../../../../../shared/errors/httpErrors";

export default class ScdlCliController extends CliController {
export default class ScdlCliController {
static cmdName = "scdl";

protected async _parse(file: string, logs: unknown[], exportDate?: Date | undefined) {
public async addProducer(producerId, producerName) {
await scdlService.createProducer({ producerId, producerName, lastUpdate: new Date() });
console.log(producerId);
}

public async parse(file: string, producerId: string, exportDate?: Date | undefined) {
if (!exportDate) throw new ExportDateError();
this.logger.logIC("\nStart parse file: ", file);
this.logger.log(`\n\n--------------------------------\n${file}\n--------------------------------\n\n`);

if (!(await scdlService.getProducer(producerId)))
throw new NotFoundError("Producer ID does not match any producer in database");

const fileContent = fs.readFileSync(file);

const grants = ScdlGrantParser.parseCsv(fileContent);

this.logger.log(`adding ${grants.length} grants in db`);

const producerId = "";
console.log(`start persisting ${grants.length} grants`);
const entities: MiscScdlGrantEntity[] = grants.map(grant => ({ ...grant, producerId: producerId }));
await scdlService.createManyGrants(entities);

this.logger.log(`parsing done`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import MiscScdlGrantEntity from "../entities/MiscScdlGrantEntity";
export class MiscScdlGrantRepository extends MigrationRepository<MiscScdlGrantEntity> {
readonly collectionName = "misc-scdl-data";

public async findAll() {
return this.collection.find({}).toArray();
}

public async createMany(entities: MiscScdlGrantEntity[]) {
this.collection.insertMany(entities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import MiscScdlProducerEntity from "../entities/MiscScdlProducerEntity";
export class MiscScdlProducersRepository extends MigrationRepository<MiscScdlProducerEntity> {
readonly collectionName = "misc-scdl-producers";

public async findByProducerId(producerId: string) {
return this.collection.findOne({ producerId });
}

public async create(entity: MiscScdlProducerEntity) {
this.collection.insertOne(entity);
return this.collection.insertOne(entity);
}
}

Expand Down
11 changes: 9 additions & 2 deletions packages/api/src/modules/providers/scdl/scdl.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import MiscScdlGrantFixture from "./__fixtures__/MiscScdlGrant";
import MiscScdlProducerFixture from "./__fixtures__/MiscScdlProducer";

describe("ScdlService", () => {
describe("createProducer", () => {
describe("getProvider()", () => {
it("should call miscScdlProducerRepository.create()", async () => {
const PRODUCER_ID = "ID";
await scdlService.getProducer(PRODUCER_ID);
expect(miscScdlProducersRepository.findByProducerId).toHaveBeenCalledWith(PRODUCER_ID);
});
});
describe("createProducer()", () => {
it("should call miscScdlProducerRepository.create()", async () => {
const PRODUCER = { ...MiscScdlProducerFixture };
await scdlService.createProducer(PRODUCER);
expect(miscScdlProducersRepository.create).toHaveBeenCalledWith(PRODUCER);
});
});
describe("createData", () => {
describe("createData()", () => {
it("should call miscScdlGrantRepository.createMany()", async () => {
const GRANTS = [{ ...MiscScdlGrantFixture, producerId: "" }];
await scdlService.createManyGrants(GRANTS);
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/modules/providers/scdl/scdl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import miscScdlGrantRepository from "./repositories/miscScdlGrant.repository";
import miscScdlProducersRepository from "./repositories/miscScdlProducer.repository";

export class ScdlService {
getProducer(producerId: string) {
return miscScdlProducersRepository.findByProducerId(producerId);
}

createProducer(entity: MiscScdlProducerEntity) {
return miscScdlProducersRepository.create(entity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SCDL CLI addProducer() should create MiscScdlProducerEntity 1`] = `
Object {
"_id": Any<ObjectId>,
"lastUpdate": Any<Date>,
"producerId": "PRODUCER_ID",
"producerName": "PRODUCER_NAME",
}
`;
exports[`SCDL CLI parse() should add grants 1`] = `Array []`;
49 changes: 49 additions & 0 deletions packages/api/tests/modules/providers/scdl/scdl.cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import path from "path";
import { ObjectId } from "mongodb";
import ScdlCliController from "../../../../src/modules/providers/scdl/interfaces/cli/scdl.cli";
import miscScdlProducersRepository from "../../../../src/modules/providers/scdl/repositories/miscScdlProducer.repository";
import { NotFoundError } from "../../../../src/shared/errors/httpErrors";
import miscScdlGrantRepository from "../../../../src/modules/providers/scdl/repositories/miscScdlGrant.repository";

const PRODUCER_ID = "PRODUCER_ID";
const PRODUCER_NAME = "PRODUCER_NAME";

// jest.useFakeTimers().setSystemTime(new Date("2023-01-01"));

describe("SCDL CLI", () => {
let cli;

beforeEach(() => {
cli = new ScdlCliController();
});
describe("addProducer()", () => {
it("should create MiscScdlProducerEntity", async () => {
await cli.addProducer(PRODUCER_ID, PRODUCER_NAME);
const document = await miscScdlProducersRepository.findByProducerId(PRODUCER_ID);
expect(document).toMatchSnapshot({ _id: expect.any(ObjectId), lastUpdate: expect.any(Date) });
});
});

describe("parse()", () => {
it("should throw NotFoundError()", async () => {
expect(() =>
cli.parse(
path.resolve(__dirname, "../../../../src/modules/providers/scdl/__fixtures__/SCDL.csv"),
"FAKE_ID",
new Date(),
),
).rejects.toThrowError(NotFoundError);
});

it("should add grants", async () => {
await cli.addProducer(PRODUCER_ID, PRODUCER_NAME);
await cli.parse(
path.resolve(__dirname, "../../../../src/modules/providers/scdl/__fixtures__/SCDL.csv"),
PRODUCER_ID,
new Date(),
);
const grants = await miscScdlGrantRepository.findAll();
expect(grants).toMatchSnapshot();
});
});
});

0 comments on commit 21453cd

Please sign in to comment.