Skip to content

Instantly share code, notes, and snippets.

@benoitguigal
Last active April 17, 2024 09:30
Show Gist options
  • Save benoitguigal/da9deb5e3aa02b4c898eb90f76fa5d07 to your computer and use it in GitHub Desktop.
Save benoitguigal/da9deb5e3aa02b4c898eb90f76fa5d07 to your computer and use it in GitHub Desktop.
import { prisma } from "@td/prisma";
import { getStream } from "../../activity-events";
import { AuthType } from "../../auth";
import Decimal from "decimal.js";
import { getBsdaRepository } from "../../bsda/repository";
const miseEnProdeDate = new Date("2024-04-09T18:00:00Z");
(async () => {
const user = await prisma.user.findUniqueOrThrow({
where: { email: "benoit.guigal@protonmail.com" }
});
const { update: updateBsda } = getBsdaRepository({
...user,
auth: AuthType.Bearer
});
// ~5400 bordereaux au total
// 1249 bordereaux affectés par le bug
const bsdas = await prisma.bsda.findMany({
where: {
// On ratisse large pour être certain de ne pas en oublier
updatedAt: { gte: miseEnProdeDate },
weightValue: { not: null },
destinationReceptionWeight: { equals: prisma.bsda.fields.weightValue }
},
select: { id: true, weightValue: true, destinationReceptionWeight: true }
});
for (const bsda of bsdas) {
// les événements soit classés par ordre chronologique
const events = await getStream(bsda.id);
// Récupère toutes les valeurs de destinationReceptionWeight qui sont
// différentes de la valeurs enregistrée
const destinationReceptionWeights = events
.map(event => event?.data?.destinationReceptionWeight)
.filter(Boolean)
.filter(
(destinationReceptionWeight: number) =>
!bsda.weightValue!.equals(new Decimal(destinationReceptionWeight))
);
if (destinationReceptionWeights.length > 0) {
// prend le dernier update s'il y en a eu plusieurs
const lastDestinationReceptionWeight =
destinationReceptionWeights.slice(-1)[0];
await updateBsda(
{ id: bsda.id },
{
destinationReceptionWeight: new Decimal(
lastDestinationReceptionWeight as number
)
}
);
}
}
})().then(() => process.exit());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment