-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add productor persistance add producerId to parse
- Loading branch information
Showing
9 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/api/src/modules/providers/scdl/entities/MiscScdlGrantEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
41 changes: 33 additions & 8 deletions
41
packages/api/src/modules/providers/scdl/interfaces/cli/scdl.cli.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 12 additions & 10 deletions
22
packages/api/src/modules/providers/scdl/interfaces/cli/scdl.cli.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/api/tests/modules/providers/scdl/__snapshots__/scdl.cli.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
packages/api/tests/modules/providers/scdl/scdl.cli.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |