Skip to content

Commit

Permalink
added function to update the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalvagno committed Sep 23, 2024
1 parent 38aa9d0 commit ad20024
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 10 deletions.
13 changes: 13 additions & 0 deletions routes/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ module.exports = {
});
},

put: async (req,res) => {
console.log("put: ", {projectId:req.params.projectId, sampleId:req.params.sampleId, sample:req.body});

return samples.update({projectId:req.params.projectId, sampleId:req.params.sampleId, sample:req.body})
.then(payload => {
return res.status(200).json(payload);
})
.catch(async(e) => {
console.error("Error:",e );
return res.status(500).json({error:e});
});
},

delete: async (req,res) => {

// console.log("list req.query:", req.query);
Expand Down
236 changes: 226 additions & 10 deletions services/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports.Samples = class {
if ( nbFractions > 0 ) {
nbScans = sample.subsample
.flatMap((subsample) => { if (subsample.scans !== undefined ) return 1; return 0;})
.reduce((a, b)=> a + b, 0); }
.reduce((a, b)=> a + b, 0);
}

const ns = {
...sample,
Expand Down Expand Up @@ -83,23 +84,40 @@ module.exports.Samples = class {
return sample
}

metadata2Array(metadata){

const metadataArray = Object.keys(metadata).map( (elem) => {

let data = {
name:elem,
value:String(metadata[elem]),
type:typeof(metadata[elem])
// convert metadata object to array of metadata
metadata2Array(metadata) {
console.debug("metadata2Array metadata: ", metadata);

const metadataArray = Object.keys(metadata).map((elem) => {
console.debug("metadata2Array elem: ", elem);
let value;

if (typeof metadata[elem] === 'number') {
console.debug('typeof(metadata[elem]) == number');
value = String(metadata[elem]);
} else if (typeof metadata[elem] === 'object') {
console.debug('typeof(metadata[elem]) == object');
value = JSON.stringify(metadata[elem]);
} else {
value = String(metadata[elem]);
}


let data = {
name: elem,
value,
type: typeof metadata[elem]
};

return data;
});

console.log('metadataArray:', metadataArray);
return metadataArray
return metadataArray;
}


async add({projectId, sample}) {

console.log("add sample: ", {projectId, sample});
Expand Down Expand Up @@ -137,6 +155,204 @@ module.exports.Samples = class {
return await this.prisma.sample.create({data})
}

// type Metadata = {
// id: number;
// name: string;
// value: string;
// type: string;
// };

// async updateMetadata(id,value,type){
async updateMetadata(meta/*:Metadata*/){
console.log("updateMetadata meta: ", meta);
return await this.prisma.metadata.update({
where: {
id: meta.id
},
data: {
value: meta.value
}
})
}

async addMetadata(name, value, type,sampleId){
const meta = {
name,
value,
type,
sampleId
}
console.log("addMetadata meta: ", meta);

return await this.prisma.metadata.create({data:meta})
}


// async update({projectId, sampleId, sample}) {
// console.log("put sample: ", {projectId, sampleId, sample});

// const oldsampledata = await this.prisma.sample.findFirst({
// where:{
// id:sampleId,
// //projectId:projectId
// },
// include:{
// metadata:true,
// metadataModel:false,
// subsample:false
// }})
// .then((payload) => {
// console.debug("Find old value:",payload)
// const metadataArray = this.metadata2Array(sample);

// const meta = payload.metadata;
// // var metaMerged = [];
// metadataArray.forEach(m => {
// if (payload[m.name] == undefined) {
// this.addMetadata(m.name, value, m.type, sampleId);
// }

// if ( meta[m.name] != payload[m.name]){
// // metaMerged[m.name]=meta[m.name]
// var newMeta/*:Metadata*/ = { ...meta[m.name] }
// newMeta.value = payload[m.name].value
// this.updateMetadata(newMeta);
// }

// });
// })
// .catch((e) => {
// console.log("Cannot get the previous state. Error: ", e);
// throw "Cannot update, Cannot get the previous state";
// });


// const metadataArray = this.metadata2Array(sample);

// // const sampleUpdated =
// console.log("metadataArray: ", metadataArray);

// return await this.prisma.sample.update({
// where: {
// id: sampleId,
// projectId: projectId
// },
// data: {
// metadata: {
// create: [
// ...metadataArray
// ]
// }
// }

// });

// // if (sample.name) {data['name'] = sample.name;}
// // if (sample.metadataModelId) {data['metadataModelId'] = sample.metadataModelId;}

// // console.log("Update: ", sampleUpdated);

// // return await this.prisma.sample.update({
// // where: {
// // id: sampleId
// // },
// // data: sampleUpdated
// // });
// }


// stringify = (value,type) => {
// if (type == "string") {
// return value;
// }
// if (type == "number") {
// return value.toString();
// }
// if (type == "boolean") {
// return value == "true";
// }
// if (type == "date") {
// return value.toISOString()
// }
// if (type == "object") {
// return JSON.stringify(value);
// }
// return value;
// }

async updateSampleMetadata(sampleId, metadataUpdates) {

var sample = metadataUpdates;

console.log("updateSampleMetadata sampleId: ", sampleId);
console.log("updateSampleMetadata metadataUpdates: ", metadataUpdates);

const existingMetadata = await this.prisma.metadata.findMany({
where: { sampleId: sampleId }
});

const updatePromises = existingMetadata.map(async (metadata) => {
console.log("updateSampleMetadata existingMetadata.map: ", metadata);

const newValue = sample[metadata.name];
// const newValue = this.stringify(sample[metadata.name], metadata.type);

console.debug('type : ', typeof newValue);

// enleve du tableau
console.debug("remove metadata.name from the array")
sample[metadata.name] = undefined;

console.log("updateSampleMetadata newValue: ", newValue);
if (newValue !== undefined && newValue !== metadata.value) {
console.log("updating metadata: ", metadata);
return this.prisma.metadata.update({
where: { id: metadata.id },
data: { value: newValue }
});

}
// }
return null;
});

console.debug("waiting updatePromises: ", updatePromises);
await Promise.all(updatePromises.filter(Boolean));
console.debug("waiting updatePromises: OK");

const addPromises = Object.entries(sample).map(async ([name, value]) => {
if (value !== undefined) {
console.log("adding metadata: ", { name, value });
return this.prisma.metadata.create({
data: {
name,
value: JSON.stringify(value),
type: typeof value,
sampleId: sampleId
}
});
}
return null;
});
console.debug("waiting addPromises: ", addPromises);
await Promise.all(addPromises.filter(Boolean));
console.debug("waiting addPromises: OK");

// return the updated sample
return await this.prisma.sample.findUnique({
where: { id: sampleId },
include: { metadata: true }
});
}


async update({projectId, sampleId, sample}) {
console.log("put sample: ", {projectId, sampleId, sample});

return this.updateSampleMetadata(sampleId,sample)
}


async deleteSample({projectId, sampleId}) {

console.log("deleteSample: ", {projectId, sampleId});
Expand Down
18 changes: 18 additions & 0 deletions zooprocess.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ paths:
security:
- BearerAuth: [ ]

put:
description: Update a sample from project
operationId: samples.put

responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Sample'
description: Return the project sample updated
'401':
$ref: '#/components/responses/UnauthorizedError'
tags:
- User
security:
- BearerAuth: [ ]

delete:
summary: Delete a sample
description: Delete a sample
Expand Down

0 comments on commit ad20024

Please sign in to comment.