Skip to content

Commit

Permalink
Change aggregates follow up
Browse files Browse the repository at this point in the history
  • Loading branch information
Riron committed Feb 25, 2025
1 parent 95dced3 commit 58b0292
Show file tree
Hide file tree
Showing 21 changed files with 1,094 additions and 248 deletions.
4 changes: 3 additions & 1 deletion back/src/registryV2/resolvers/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { registryUploadSignedUrl } from "./queries/registryUploadSignedUrl";
import { registryDownloadSignedUrl } from "./queries/registryDownloadSignedUrl";
import { registryImports } from "./queries/registryImports";
import { registryImport } from "./queries/registryImport";
import { registryChangeAggregates } from "./queries/registryChangeAggregates";

export const Query: QueryResolvers = {
registryUploadSignedUrl,
Expand All @@ -14,5 +15,6 @@ export const Query: QueryResolvers = {
registryImport: registryImport as any,
registryV2ExportDownloadSignedUrl,
registryV2Exports: registryV2Exports as any,
registryV2Export: registryV2Export as any
registryV2Export: registryV2Export as any,
registryChangeAggregates: registryChangeAggregates as any
};
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,73 @@ describe("Registry - addToIncomingTexsRegistry", () => {
});
expect(result.wasteCodeBale).toBe("A1070");
});

it("should create a ChangeAggregate when creating several incoming texs items", async () => {
const { user, company } = await userWithCompanyFactory();
const { mutate } = makeClient(user);

const lines = Array.from({ length: 100 }, () =>
getCorrectLine(company.siret!)
);

const { data } = await mutate<Pick<Mutation, "addToIncomingTexsRegistry">>(
ADD_TO_INCOMING_TEXS_REGISTRY,
{ variables: { lines } }
);

expect(data.addToIncomingTexsRegistry).toBe(true);

const changeAggregates = await prisma.registryChangeAggregate.findMany({
where: { createdById: user.id }
});

expect(changeAggregates.length).toBe(1);
expect(changeAggregates[0].numberOfInsertions).toBe(100);
});

it("should amend to previous ChangeAggregate when creating incoming texs items less than 1 min after previous creation", async () => {
const { user, company } = await userWithCompanyFactory();
const { mutate } = makeClient(user);

const lines = Array.from({ length: 100 }, () =>
getCorrectLine(company.siret!)
);

const { data } = await mutate<Pick<Mutation, "addToIncomingTexsRegistry">>(
ADD_TO_INCOMING_TEXS_REGISTRY,
{ variables: { lines } }
);

expect(data.addToIncomingTexsRegistry).toBe(true);

const changeAggregates = await prisma.registryChangeAggregate.findMany({
where: { createdById: user.id }
});

expect(changeAggregates.length).toBe(1);
expect(changeAggregates[0].numberOfInsertions).toBe(100);
});

it("should create a ChangeAggregate when creating incoming texs items more than 1 min after previous creation", async () => {
const { user, company } = await userWithCompanyFactory();
const { mutate } = makeClient(user);

const lines = Array.from({ length: 100 }, () =>
getCorrectLine(company.siret!)
);

const { data } = await mutate<Pick<Mutation, "addToIncomingTexsRegistry">>(
ADD_TO_INCOMING_TEXS_REGISTRY,
{ variables: { lines } }
);

expect(data.addToIncomingTexsRegistry).toBe(true);

const changeAggregates = await prisma.registryChangeAggregate.findMany({
where: { createdById: user.id }
});

expect(changeAggregates.length).toBe(1);
expect(changeAggregates[0].numberOfInsertions).toBe(100);
});
});
19 changes: 12 additions & 7 deletions back/src/registryV2/resolvers/mutations/genericAddToRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { pluralize } from "@td/constants";
import { prisma } from "@td/prisma";
import {
UNAUTHORIZED_ERROR,
isAuthorized,
ImportType,
RegistryChanges,
UNAUTHORIZED_ERROR,
importOptions,
incrementLocalChangesForCompany,
saveCompaniesChanges,
ImportType,
importOptions
isAuthorized,
saveCompaniesChanges
} from "@td/registry";
import { UserInputError } from "../../../common/errors";
import { checkIsAuthenticated } from "../../../common/permissions";
Expand Down Expand Up @@ -68,7 +68,10 @@ export async function genericAddToRegistry<T extends UnparsedLine>(

const { safeParseAsync, saveLine } = options;
const errors = new Map<string, string>();
const changesByCompany = new Map<string, RegistryChanges>();
const changesByCompany = new Map<
string,
{ [reportAsSiret: string]: RegistryChanges }
>();

for (const line of lines) {
const result = await safeParseAsync(line);
Expand All @@ -90,7 +93,9 @@ export async function genericAddToRegistry<T extends UnparsedLine>(

incrementLocalChangesForCompany(changesByCompany, {
reason: result.data.reason,
reportForCompanySiret: result.data.reportForCompanySiret
reportForCompanySiret: result.data.reportForCompanySiret,
reportAsCompanySiret:
result.data.reportAsCompanySiret ?? result.data.reportForCompanySiret
});

await saveLine({
Expand Down
Loading

0 comments on commit 58b0292

Please sign in to comment.