Skip to content

Commit 9659d3e

Browse files
authored
Merge pull request #46 from haniffalab/fix/dataset_id
handle admin or plugin study update to trigger dataset udpate
2 parents 3b97fc7 + 30ec50d commit 9659d3e

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

src/api/dataset/content-types/dataset/lifecycles.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ module.exports = {
3131
});
3232
}
3333

34-
event.params.data.dataset_id = (data.study?.connect?.[0] || entry.study?.id || null) + ':' + (event.params.data.uid || entry.uid);
34+
const {connect, disconnect} = data.study || {};
35+
let study_id = entry.study?.id || null;
36+
if (connect?.length) {
37+
study_id = connect[0].id;
38+
}
39+
if (disconnect?.length && disconnect[0].id === study_id) {
40+
study_id = null;
41+
}
42+
event.params.data.dataset_id = (study_id) + ':' + (event.params.data.uid || entry.uid);
3543
}
3644
},
3745
};

src/api/study/content-types/study/lifecycles.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,29 @@ module.exports = {
4343
event.state = data.datasets;
4444
},
4545
// Update datasets' dataset_id when connecting/disconnecting to/from study
46+
47+
// If created through admin event.state will be an object with connect, disconnect
48+
// If created through import plugin datasets will be an array of ids
4649
async afterCreate(event) {
47-
const datasets = event.state;
50+
let datasets = event.state;
51+
if (!Array.isArray(datasets)){
52+
datasets = datasets.connect?.map((d) => d.id) || [];
53+
}
4854
for (const idx in datasets) {
4955
await strapi.entityService.update('api::dataset.dataset', datasets[idx], {
50-
data: { study: event.result.id }
56+
data: {} // study id will already be added to the dataset
5157
});
5258
}
5359
},
5460
async afterUpdate(event) {
55-
const { disconnect, connect } = event.state;
56-
for (const idx in disconnect) {
57-
await strapi.entityService.update('api::dataset.dataset', disconnect[idx].id, {
58-
data: { study: null }
59-
});
61+
let datasets = event.state;
62+
if (!Array.isArray(datasets)){
63+
const {connect = [], disconnect = []} = datasets;
64+
datasets = [...connect.map((d) => d.id), ...disconnect.map((d) => d.id)];
6065
}
61-
for (const idx in connect) {
62-
await strapi.entityService.update('api::dataset.dataset', connect[idx].id, {
63-
data: { study: event.result.id }
66+
for (const idx in datasets) {
67+
await strapi.entityService.update('api::dataset.dataset', datasets[idx], {
68+
data: {} // study id will already be added to the dataset
6469
});
6570
}
6671
}

0 commit comments

Comments
 (0)