Skip to content

Commit

Permalink
fix: fix mino (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
iNeoO authored Oct 8, 2024
2 parents b57b6f8 + 18b6284 commit 18f85c9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
12 changes: 10 additions & 2 deletions packages/backend/src/controllers/documents/upload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require("node:fs/promises");
const DocumentService = require("../../services/Document");
const AppError = require("../../utils/error");

Expand Down Expand Up @@ -41,8 +42,15 @@ module.exports = async (req, res, next) => {
}

try {
const uuid = await DocumentService.uploadLegacy(category, file);
await DocumentService.upload(category, file, uuid);
const { path, originalname: filename, mimetype } = file;
const data = await fs.readFile(path);
const uuid = await DocumentService.uploadLegacy(
filename,
category,
mimetype,
data,
);
await DocumentService.upload(filename, category, mimetype, data, uuid);
log.d("DONE", uuid);
return res.json({ uuid });
} catch (error) {
Expand Down
35 changes: 20 additions & 15 deletions packages/backend/src/services/Document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-param-reassign */
const fs = require("node:fs/promises");
const logger = require("../utils/logger");
const poolDoc = require("../utils/pgpoolDoc").getPool();
const AppError = require("../utils/error");
Expand Down Expand Up @@ -82,16 +80,14 @@ module.exports.download = async (uuid) => {
}
};

module.exports.uploadLegacy = async (category, file) => {
module.exports.uploadLegacy = async (filename, category, mimetype, data) => {
log.i("uploadLegacy - In");
try {
const { path, originalname: filename } = file;
const data = await fs.readFile(path);
log.d("uploadLegacy", category, filename);
const {
rows: [{ uuid }],
} = await poolDoc.query(
...query.create(category, filename, file.mimetype, data),
...query.create(category, filename, mimetype, data),
);
log.d("uploadLegacy - Done");
return uuid;
Expand All @@ -101,11 +97,15 @@ module.exports.uploadLegacy = async (category, file) => {
}
};

module.exports.upload = async (category, file, uuid = crypto.randomUUID()) => {
module.exports.upload = async (
filename,
category,
mimetype,
data,
uuid = crypto.randomUUID(),
) => {
log.i("upload - In");
try {
const { path, originalname: filename } = file;
const data = await fs.readFile(path);
log.d("upload", category, filename);
await s3Client.send(
new PutObjectCommand({
Expand All @@ -115,7 +115,7 @@ module.exports.upload = async (category, file, uuid = crypto.randomUUID()) => {
Metadata: {
category,
created_at: String(new Date()),
mimetype: file.mimetype,
mimetype: mimetype,
originalname: filename,
},
}),
Expand All @@ -127,20 +127,25 @@ module.exports.upload = async (category, file, uuid = crypto.randomUUID()) => {
}
};

module.exports.createFile = async (filename, category, typeMime, data) => {
log.i("createFile - In");
module.exports.createFileLegacy = async (
filename,
category,
typeMime,
data,
) => {
log.i("createFile pg - In");
try {
log.i("createFile", { category, filename, typeMime });
log.i("createFile pg", { category, filename, typeMime });
const {
rows: [{ uuid }],
} = await poolDoc.query(
...query.create(category, filename, typeMime, data),
);
log.i("createFile - Done");
log.i("createFile pg - Done");
return uuid;
} catch (err) {
log.w(err);
throw new AppError("createFile failed", { cause: err });
throw new AppError("createFile pg failed", { cause: err });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ const generate = async (declaration) => {
const buffer = await build(declaration ?? {});

// insert into documents table
const uuid = await Document.createFile(
const uuid = await Document.createFileLegacy(
`AR_${declaration.idFonctionnelle}.pdf`,
"AR_declaration_2_mois",
"application/pdf",
buffer,
);
await Document.upload(
`AR_${declaration.idFonctionnelle}.pdf`,
"AR_declaration_2_mois",
"application/pdf",
buffer,
uuid,
);
log.d(`http://localhost:3010/documents/${uuid}`);

// insert into demande_sejour table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ const generate = async (declaration) => {
const buffer = await build(declaration ?? {});

// insert into documents table
const uuid = await Document.createFile(
const uuid = await Document.createFileLegacy(
`AR_${declaration.idFonctionnelle}_8J.pdf`,
"AR_declaration_8_jours",
"application/pdf",
buffer,
);
await Document.upload(
`AR_${declaration.idFonctionnelle}_8J.pdf`,
"AR_declaration_8_jours",
"application/pdf",
buffer,
uuid,
);
log.d(`http://localhost:3010/documents/${uuid}`);

// insert into demande_sejour table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ const generate = async (declaration, idFonctionnelle, departementSuivi) => {
const buffer = await build(declaration ?? {}, departementSuivi);

// insert into documents table
const uuid = await Document.createFile(
const uuid = await Document.createFileLegacy(
`${idFonctionnelle}.pdf`,
"declaration_2_mois",
"application/pdf",
buffer,
);
await Document.upload(
`${idFonctionnelle}.pdf`,
"declaration_2_mois",
"application/pdf",
buffer,
uuid,
);
log.d(`http://localhost:3010/documents/${uuid}`);

// insert into demande_sejour table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ const generate = async (
);

// insert into documents table
const uuid = await Document.createFile(
const uuid = await Document.createFileLegacy(
`${idFonctionnelle}_8jours.pdf`,
"declaration_8jours",
"application/pdf",
buffer,
);
await Document.upload(
`${idFonctionnelle}_8jours.pdf`,
"declaration_8jours",
"application/pdf",
buffer,
uuid,
);
log.d(`http://localhost:3010/documents/${uuid}`);

// insert into demande_sejour table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@
name="effectifPrevisionnel"
:label="`${DeclarationSejour.isPost8Jour(declarationStatut) ? 'Effectif des vacanciers' : 'Effectif prévisionnel des vacanciers'}`"
:label-visible="true"
:model-value="
effectifPrevisionnel ??
(parseInt(effectifPrevisionnelFemme, 10) || 0) +
(parseInt(effectifPrevisionnelHomme, 10) || 0)
"
:readonly="!props.modifiable"
:is-valid="effectifPrevisionnelMeta.valid"
:error-message="effectifPrevisionnelErrorMessage"
:model-value="effectifs"
placeholder="5"
readonly
disabled
/>
</div>
Expand Down Expand Up @@ -138,11 +132,6 @@ const { meta, values } = useForm({
validateOnMount: props.validateOnMount,
});
const {
value: effectifPrevisionnel,
errorMessage: effectifPrevisionnelErrorMessage,
meta: effectifPrevisionnelMeta,
} = useField("effectifPrevisionnel");
const {
value: effectifPrevisionnelHomme,
errorMessage: effectifPrevisionnelHommeErrorMessage,
Expand All @@ -166,6 +155,16 @@ const {
meta: precisionDeficiencesMeta,
} = useField("precisionDeficiences");
const effectifs = computed(() => {
const total =
(parseInt(effectifPrevisionnelFemme.value, 10) || 0) +
(parseInt(effectifPrevisionnelHomme.value, 10) || 0);
if (total === initialValues.effectifPrevisionnel) {
return initialValues.effectifPrevisionnel ?? 0;
}
return total;
});
function next() {
if (!meta.value.dirty || !props.modifiable) {
return emit("next");
Expand All @@ -175,12 +174,7 @@ function next() {
{
...values,
meta: meta.value.valid,
effectifPrevisionnel:
effectifPrevisionnel.value ??
String(
(parseInt(effectifPrevisionnelHomme.value, 10) || 0) +
(parseInt(effectifPrevisionnelFemme.value, 10) || 0),
),
effectifPrevisionnel: effectifs.value,
},
"informationsVacanciers",
);
Expand Down

0 comments on commit 18f85c9

Please sign in to comment.