Skip to content

Commit

Permalink
fix(document): cannot modify or delete files
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarseguerra committed May 30, 2024
1 parent 508b3be commit be14424
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
15 changes: 8 additions & 7 deletions api/controllers/v1/document/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ module.exports = async (req, res) => {

const out = {
document: toDocument(newDoc),
status: errorFiles
? {
errorCode: 'FileNotImported',
errorString: 'Some files were not imported.',
content: errorFiles,
}
: undefined,
status:
errorFiles.length > 0
? {
errorCode: 'FileNotImported',
errorString: 'Some files were not imported.',
content: errorFiles,
}
: undefined,
};

return ControllerService.treat(
Expand Down
10 changes: 10 additions & 0 deletions api/controllers/v1/document/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ module.exports = async (req, res) => {
authorId
);

const modifiedFiles = DocumentService.getChangedFileFromClient(
req.body.modifiedFiles ?? []
);

const deletedFiles = DocumentService.getChangedFileFromClient(
req.body.deletedFiles ?? []
);

const updatedDocument = await DocumentService.updateDocument({
documentId: req.param('id'),
reviewerId: authorId,
documentData,
descriptionData,
newFiles,
modifiedFiles,
deletedFiles,
});
if (!updatedDocument) return res.notFound();

Expand Down
10 changes: 8 additions & 2 deletions api/services/DocumentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,22 @@ module.exports = {
language: body.mainLanguage,
}),

getChangedFileFromClient: (fileObjectArray) =>
fileObjectArray.map((e) => ({
id: e.id,
fileName: e.fileName,
})),

// Extract everything from the request body except id and dateInscription
// Used when creating or editing an existing document
getConvertedDataFromClient: async (body) => {
// Massif will be deleted in the future (a document can be about many massifs and a massif can be the subject of many documents): use massifs
const massif = body.massif?.id;
const massifs = [...(body.massifs ?? []), ...(massif ? [massif] : [])];

const optionFound = undefined;
let optionFound;
// eslint-disable-next-line no-param-reassign
if (body.option) body.option = await TOption.findOne({ name: body.option });
if (body.option) optionFound = await TOption.findOne({ name: body.option });
let typeFound;
if (body.type) typeFound = await TType.findOne({ name: body.type });

Expand Down
37 changes: 21 additions & 16 deletions api/services/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,22 @@ module.exports = {
}

if (!credentials) {
return noCredentialWarning('Document upload', {
noCredentialWarning('Document upload', {
name,
mimeType,
size: file.size,
});
}

sails.log.info(`Uploading ${name} to Azure Blob...`);
try {
const blockBlobClient =
credentials.documentsBlobClient.getBlockBlobClient(pathName);
await blockBlobClient.uploadData(file.buffer, {
blobHTTPHeaders: { blobContentType: mimeType },
});
} catch (err) {
throw new FileError(ERROR_DURING_UPLOAD_TO_AZURE, name);
} else {
sails.log.info(`Uploading ${name} to Azure Blob...`);
try {
const blockBlobClient =
credentials.documentsBlobClient.getBlockBlobClient(pathName);
await blockBlobClient.uploadData(file.buffer, {
blobHTTPHeaders: { blobContentType: mimeType },
});
} catch (err) {
throw new FileError(ERROR_DURING_UPLOAD_TO_AZURE, name);
}
}

const param = {
Expand All @@ -157,11 +157,16 @@ module.exports = {
},

async delete(file) {
const blockBlobClient =
credentials.documentsBlobClient.getBlockBlobClient(file.path);
await blockBlobClient.delete({ deleteSnapshots: 'include' });

const destroyedRecord = await TFile.destroyOne(file.id);
if (!credentials) {
noCredentialWarning('Document delete', file);
} else {
const blockBlobClient =
credentials.documentsBlobClient.getBlockBlobClient(
destroyedRecord.path
);
await blockBlobClient.delete({ deleteSnapshots: 'include' });
}
return destroyedRecord;
},
},
Expand Down
7 changes: 4 additions & 3 deletions api/services/mapping/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ const c = {
},

// Only present when it is a modified document
modifiedFiles: source.modifiedFiles,
newFiles: source.newFiles,
deletedFiles: source.deletedFiles,
newFiles: toList('newFiles', source, c.toFile),
modifiedFiles: toList('modifiedFiles', source, c.toFile),
deletedFiles: toList('deletedFiles', source, c.toFile),
};

// snapshot
Expand Down Expand Up @@ -500,6 +500,7 @@ const c = {
},

toFile: (source) => ({
id: source.id,
dateInscription: source.dateInscription,
isValidated: source.isValidated,
fileName: source.fileName,
Expand Down

0 comments on commit be14424

Please sign in to comment.