Skip to content

Commit ae34bf3

Browse files
authored
Merge pull request #41 from haniffalab/patch/data-import
update collection and team to support the data-import plugin
2 parents 00ea2c8 + 8c939d8 commit ae34bf3

7 files changed

Lines changed: 113 additions & 5 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
async beforeCreate(event) {
3+
event.params.data.uid = await strapi.service('plugin::content-manager.uid')
4+
.generateUIDField({
5+
contentTypeUID: 'api::collection.collection',
6+
field: 'uid',
7+
data: event.params.data
8+
});
9+
},
10+
async beforeUpdate(event) {
11+
const { data, where } = event.params;
12+
13+
const isPublishAction = 'publishedAt' in data;
14+
15+
if (!isPublishAction){
16+
const entry = await strapi.entityService.findOne('api::collection.collection', where.id);
17+
18+
if ('name' in data && data.name !== entry.name){
19+
event.params.data.uid = await strapi.service('plugin::content-manager.uid')
20+
.generateUIDField({
21+
contentTypeUID: 'api::collection.collection',
22+
field: 'uid',
23+
data: data
24+
});
25+
}
26+
}
27+
},
28+
29+
};

src/api/collection/content-types/collection/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
"name": {
2929
"type": "string",
3030
"unique": true
31+
},
32+
"uid": {
33+
"type": "uid",
34+
"targetField": "name",
35+
"required": true
3136
}
3237
}
3338
}

src/api/collection/services/collection.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,24 @@
66

77
const { createCoreService } = require('@strapi/strapi').factories;
88

9-
module.exports = createCoreService('api::collection.collection');
9+
module.exports = createCoreService('api::collection.collection', ({strapi}) => ({
10+
async findByUid(ctx){
11+
const ctUid = 'api::collection.collection';
12+
const attrs = strapi.contentTypes[ctUid].__schema__.attributes;
13+
const uidTarget =attrs['uid'].targetField;
14+
const { [uidTarget]: uidTargetValue } = ctx.params;
15+
16+
const populateParams = strapi.config.functions.getPopulateParams(attrs);
17+
18+
let entity = await strapi.db.query(ctUid).findOne({
19+
where: { [uidTarget]: uidTargetValue },
20+
populate: populateParams
21+
});
22+
23+
if (entity){
24+
entity = strapi.config.functions.reduceComponentData(attrs, entity);
25+
}
26+
27+
return entity;
28+
}
29+
}));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
async beforeCreate(event) {
3+
event.params.data.uid = await strapi.service('plugin::content-manager.uid')
4+
.generateUIDField({
5+
contentTypeUID: 'api::team.team',
6+
field: 'uid',
7+
data: event.params.data
8+
});
9+
},
10+
async beforeUpdate(event) {
11+
const { data, where } = event.params;
12+
13+
const isPublishAction = 'publishedAt' in data;
14+
15+
if (!isPublishAction){
16+
const entry = await strapi.entityService.findOne('api::team.team', where.id);
17+
18+
if ('name' in data && data.name !== entry.name){
19+
event.params.data.uid = await strapi.service('plugin::content-manager.uid')
20+
.generateUIDField({
21+
contentTypeUID: 'api::team.team',
22+
field: 'uid',
23+
data: data
24+
});
25+
}
26+
}
27+
},
28+
};

src/api/team/content-types/team/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
},
4343
"email": {
4444
"type": "email"
45+
},
46+
"uid": {
47+
"type": "uid",
48+
"targetField": "name",
49+
"required": true
4550
}
4651
}
4752
}

src/api/team/services/team.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,24 @@
66

77
const { createCoreService } = require('@strapi/strapi').factories;
88

9-
module.exports = createCoreService('api::team.team');
9+
module.exports = createCoreService('api::team.team', ({strapi}) => ({
10+
async findByUid(ctx){
11+
const ctUid = 'api::team.team';
12+
const attrs = strapi.contentTypes[ctUid].__schema__.attributes;
13+
const uidTarget =attrs['uid'].targetField;
14+
const { [uidTarget]: uidTargetValue } = ctx.params;
15+
16+
const populateParams = strapi.config.functions.getPopulateParams(attrs);
17+
18+
let entity = await strapi.db.query(ctUid).findOne({
19+
where: { [uidTarget]: uidTargetValue },
20+
populate: populateParams
21+
});
22+
23+
if (entity){
24+
entity = strapi.config.functions.reduceComponentData(attrs, entity);
25+
}
26+
27+
return entity;
28+
}
29+
}));

src/plugins/data-import/server/content-types/datafile/import.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { ApplicationError } = require('@strapi/utils').errors;
34
const array = require('lodash/array');
45
const collection = require('lodash/collection');
56
const object = require('lodash/object');
@@ -22,10 +23,10 @@ const getEbiFieldData = async (data, ontology) => {
2223
({options} = res);
2324

2425
if (error) {
25-
throw new Error(error);
26+
throw new ApplicationError(error);
2627
}
2728
else if (!options || !options.length){
28-
throw new Error(`Provided data ${q} does not match to any ontology`);
29+
throw new ApplicationError(`Provided data ${q} does not match to any ontology`);
2930
}
3031

3132
if (label){
@@ -34,7 +35,7 @@ const getEbiFieldData = async (data, ontology) => {
3435
);
3536
}
3637
if (options.length > 1){
37-
throw new Error(`Provided data ${q} does not match to a single ontology`);
38+
throw new ApplicationError(`Provided data ${q} does not match to a single ontology`);
3839
}
3940

4041
return options[0];

0 commit comments

Comments
 (0)