|
|
@@ -16,95 +16,115 @@ module.exports = createCoreController('api::material.material',({ strapi }) => |
|
|
|
}, |
|
|
|
async create(ctx) { |
|
|
|
const data = JSON.parse(ctx.request.body.data); |
|
|
|
if(data["Start_Date"])data["Start_Date"]= moment(data['Start_Date'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
if(data["End_Date"])data["End_Date"]= moment(data['End_Date'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
if(data["Presented"])data["Presented"]= moment(data['Presented'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
var matPintar; |
|
|
|
// if(data.Category == 'Pintar'){ |
|
|
|
if(data.withSummary && data.withSummary == 'y'){ |
|
|
|
data.By_Admin = true; |
|
|
|
const companyName = await strapi.entityService.findOne('api::company.company', data.company); |
|
|
|
const divisionName = await strapi.entityService.findOne('api::division.division', data.division); |
|
|
|
// console.log(companyName,divisionName); |
|
|
|
matPintar = await strapi.entityService.create('api::pintar-summary.pintar-summary', { |
|
|
|
data: { |
|
|
|
Company : companyName?companyName.Name:data.company, |
|
|
|
Division : divisionName?divisionName.Name:data.division, |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
if(data.division && data.division != 'All' && data.company && data.company != 'All'){ |
|
|
|
if(data.division && data.company){ |
|
|
|
if(data["Start_Date"])data["Start_Date"]= moment(data['Start_Date'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
if(data["End_Date"])data["End_Date"]= moment(data['End_Date'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
if(data["Presented"])data["Presented"]= moment(data['Presented'],'MM/DD/YYYY').format('YYYY-MM-DD') |
|
|
|
var listCompany = data.company.split(','); |
|
|
|
var listDivisi = data.division.split(','); |
|
|
|
var matPintar; |
|
|
|
|
|
|
|
// if(data.Category == 'Pintar'){ |
|
|
|
if(data.withSummary && data.withSummary == 'y'){ |
|
|
|
if(matPintar)data["pintar_summary"] = matPintar.id; |
|
|
|
data.By_Admin = true; |
|
|
|
const companyName = await strapi.entityService.findMany('api::company.company', { |
|
|
|
filters:{ |
|
|
|
id:{ |
|
|
|
$in: listCompany, |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
const divisionName = await strapi.entityService.findMany('api::division.division', { |
|
|
|
filters:{ |
|
|
|
id:{ |
|
|
|
$in: listDivisi, |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
// console.log(companyName.map(i=>i.Name).join(' , '),divisionName.map(i=>i.Name).join(' , ')); |
|
|
|
matPintar = await strapi.entityService.create('api::pintar-summary.pintar-summary', { |
|
|
|
data: { |
|
|
|
Company : companyName && companyName.length>0?companyName.map(i=>i.Name).join(' , '):data.company, |
|
|
|
Division : divisionName&& divisionName.length>0?divisionName.map(i=>i.Name).join(' , '):data.division, |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
ctx.request.body.data = JSON.stringify(data); |
|
|
|
const response = await super.create(ctx); |
|
|
|
if(response.data.id){ |
|
|
|
console.log('participant Luisr',data.ParticipantList); |
|
|
|
var participants = (data.ParticipantList) |
|
|
|
?(typeof data.ParticipantList == "string")?JSON.parse(data.ParticipantList):data.ParticipantList |
|
|
|
:[]; |
|
|
|
if(participants && participants.length>0){ |
|
|
|
for(var i=0;i<participants.length;i++){ |
|
|
|
var item = participants[i]; |
|
|
|
var keys = Object.keys(item); |
|
|
|
await strapi.query('api::participant.participant').create({ |
|
|
|
data: { |
|
|
|
Email: item[keys.filter((key)=>key.toUpperCase().includes('EMAIL'))[0]], |
|
|
|
Name : item[keys.filter((key)=>key.toUpperCase().includes('NAMA'))[0]], |
|
|
|
Division : item[keys.filter((key)=>key.toUpperCase().includes('DIVISI'))[0]], |
|
|
|
Company : item[keys.filter((key)=>key.toUpperCase().includes('COMPANY'))[0]], |
|
|
|
material : response.data.id |
|
|
|
} |
|
|
|
}); |
|
|
|
if(listDivisi.length==1 && !listDivisi.includes('All') && listCompany.length==1 && !listCompany.includes('All')){ |
|
|
|
if(data.withSummary && data.withSummary == 'y'){ |
|
|
|
if(matPintar)data["pintar_summary"] = matPintar.id; |
|
|
|
} |
|
|
|
ctx.request.body.data = JSON.stringify(data); |
|
|
|
const response = await super.create(ctx); |
|
|
|
if(response.data.id){ |
|
|
|
// console.log('participant Luisr',data.ParticipantList); |
|
|
|
var participants = (data.ParticipantList) |
|
|
|
?(typeof data.ParticipantList == "string")?JSON.parse(data.ParticipantList):data.ParticipantList |
|
|
|
:[]; |
|
|
|
if(participants && participants.length>0){ |
|
|
|
for(var i=0;i<participants.length;i++){ |
|
|
|
var item = participants[i]; |
|
|
|
var keys = Object.keys(item); |
|
|
|
await strapi.query('api::participant.participant').create({ |
|
|
|
data: { |
|
|
|
Email: item[keys.filter((key)=>key.toUpperCase().includes('EMAIL'))[0]], |
|
|
|
Name : item[keys.filter((key)=>key.toUpperCase().includes('NAMA'))[0]], |
|
|
|
Division : item[keys.filter((key)=>key.toUpperCase().includes('DIVISI'))[0]], |
|
|
|
Company : item[keys.filter((key)=>key.toUpperCase().includes('COMPANY'))[0]], |
|
|
|
material : response.data.id |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
else{ |
|
|
|
if(data.withSummary && data.withSummary == 'y'){ |
|
|
|
if(matPintar)data["pintar_summary"] = matPintar.id; |
|
|
|
} |
|
|
|
var divFilter = (data.division=='All')?{}:{id:{$eq: data.division,}}; |
|
|
|
const entity = await strapi.entityService.findMany('api::division.division', { |
|
|
|
fields: ['Name'], |
|
|
|
populate: { companies: true }, |
|
|
|
filters:divFilter |
|
|
|
}); |
|
|
|
const companyFilter = data.company; |
|
|
|
var response,attachments,thumbnail; |
|
|
|
for(var i=0;i<entity.length;i++){ |
|
|
|
var companies = entity[i].companies; |
|
|
|
if(companyFilter!='All'){ |
|
|
|
companies = companies.filter(i=>i.id==data.company); |
|
|
|
else{ |
|
|
|
if(data.withSummary && data.withSummary == 'y'){ |
|
|
|
if(matPintar)data["pintar_summary"] = matPintar.id; |
|
|
|
} |
|
|
|
for(var j=0;j<companies.length;j++){ |
|
|
|
data.company = companies[j].id; |
|
|
|
data.division = entity[i].id; |
|
|
|
if(i==0 && j==0){ |
|
|
|
ctx.request.body.data = JSON.stringify(data); |
|
|
|
// console.log(ctx.request.body.data); |
|
|
|
response = await super.create(ctx); |
|
|
|
// console.log(response); |
|
|
|
const mat = await strapi.entityService.findOne('api::material.material', response.data.id, { |
|
|
|
populate:['Thumbnail','Attachments'] |
|
|
|
}); |
|
|
|
attachments = mat.Attachments.map(i=>i.id); |
|
|
|
thumbnail = mat.Thumbnail.id; |
|
|
|
var divFilter = (listDivisi.includes('All'))?{}:{id:{$in: listDivisi,}}; |
|
|
|
const entity = await strapi.entityService.findMany('api::division.division', { |
|
|
|
fields: ['Name'], |
|
|
|
populate: { companies: true }, |
|
|
|
filters:divFilter |
|
|
|
}); |
|
|
|
// const companyFilter = data.company; |
|
|
|
var response,attachments,thumbnail; |
|
|
|
for(var i=0;i<entity.length;i++){ |
|
|
|
var companies = entity[i].companies; |
|
|
|
if(!listCompany.includes('All')){ |
|
|
|
companies = companies.filter(i=>listCompany.includes(i.id.toString())); |
|
|
|
} |
|
|
|
else{ |
|
|
|
data["Attachments"]=attachments; |
|
|
|
data["Thumbnail"]=thumbnail; |
|
|
|
data['publishedAt'] = moment().format('YYYY-MM-DD'); |
|
|
|
response = await strapi.entityService.create('api::material.material',{ |
|
|
|
data:data |
|
|
|
}).catch(e=>console.log(e)); |
|
|
|
for(var j=0;j<companies.length;j++){ |
|
|
|
data.company = companies[j].id; |
|
|
|
data.division = entity[i].id; |
|
|
|
if(i==0 && j==0){ |
|
|
|
ctx.request.body.data = JSON.stringify(data); |
|
|
|
// console.log(ctx.request.body.data); |
|
|
|
response = await super.create(ctx); |
|
|
|
// console.log(response); |
|
|
|
const mat = await strapi.entityService.findOne('api::material.material', response.data.id, { |
|
|
|
populate:['Thumbnail','Attachments'] |
|
|
|
}); |
|
|
|
attachments = mat.Attachments.map(i=>i.id); |
|
|
|
thumbnail = mat.Thumbnail.id; |
|
|
|
} |
|
|
|
else{ |
|
|
|
data["Attachments"]=attachments; |
|
|
|
data["Thumbnail"]=thumbnail; |
|
|
|
data['publishedAt'] = moment().format('YYYY-MM-DD'); |
|
|
|
response = await strapi.entityService.create('api::material.material',{ |
|
|
|
data:data |
|
|
|
}).catch(e=>console.log(e)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
} |
|
|
|
else{ |
|
|
|
return ctx.badRequest('Company dan Divisi harus diisi!'); |
|
|
|
} |
|
|
|
}, |
|
|
|
async update(ctx) { |
|
|
|
const editable = await strapi.entityService.findOne('api::editable.editable', 1, { |
|
|
|