Skip to content

Commit

Permalink
fix: import
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxvd committed Dec 14, 2023
1 parent 92a4320 commit eab69c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 40 deletions.
37 changes: 1 addition & 36 deletions server/initializers/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,40 +563,6 @@ function generateApiUpdate (fields) {
}
}

function generateImportData (form) {
const importSkipFields = [
'id',
'pictures',
'track',
'moderatorReview',
'startDate',
'startTime',
'endDate',
'endTime',
'observationDate',
'observationTime'
]

return async function (data) {
_.forEach(data, (value, name) => {
if (value === '') return
if (importSkipFields.includes(name)) return

this[name] = value
if (name.endsWith('Local') && data.language && data.language !== 'en') {
this[`${name.substring(0, name.length - 5)}Lang`] = data.language || null
}
})

this.startDateTime = data.startDateTime || moment(data.startDate + ' ' + data.startTime, api.config.formats.date + ' ' + api.config.formats.time).tz(api.config.formats.tz).toDate()
this.endDateTime = data.endDateTime || moment(data.endDate + ' ' + data.endTime, api.config.formats.date + ' ' + api.config.formats.time).tz(api.config.formats.tz).toDate()
this.observationDateTime = data.observationDateTime || moment(data.observationDate + ' ' + data.observationTime, api.config.formats.date + ' ' + api.config.formats.time).tz(api.config.formats.tz).toDate()
this.userId = data.userId || data.user

return this
}
}

function formOptions (form) {
return {
freezeTableName: true,
Expand All @@ -618,8 +584,7 @@ function formOptions (form) {
calculateHash: generateCalcHash(form.fields),
apiData: generateApiData(form.fields),
apiUpdate: generateApiUpdate(form.fields),
exportData: generateExportData(form),
importData: generateImportData(form)
exportData: generateExportData(form)
},
hooks: form.hooks,
validate: form.validate
Expand Down
6 changes: 5 additions & 1 deletion server/tasks/formImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ module.exports = class FormImport extends Task {

try {
const form = api.forms[formName]
let userAllowed = false
if (api.forms.userCanManage(user, form.modelName)) {
userAllowed = true
}

await api.sequelize.sequelize.transaction(async (t) => {
for (let i = 0; i < params.items.length; i++) {
try {
const itemData = prepareImportData(
params.items[i],
params.user,
(userAllowed ? params.items[i]?.userId : user.id) || user.id,
params.language,
user.organizationSlug
)
Expand Down
7 changes: 4 additions & 3 deletions server/utils/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash')
const moment = require('moment/moment')
const { api } = require('actionhero')

const prepareImportData = (data, user, language, organization) => {
const prepareImportData = (data, userId, language, organization) => {
const importSkipFields = [
'id',
'pictures',
Expand All @@ -17,7 +17,7 @@ const prepareImportData = (data, user, language, organization) => {
]

const importItem = {
user,
userId,
language,
organization
}
Expand Down Expand Up @@ -67,7 +67,8 @@ const prepareImportData = (data, user, language, organization) => {
: importItem.observationDateTime
}

importItem.userId = data.userId || data.user
importItem.userId = userId
importItem.user = userId
importItem.monitoringCode = data.monitoringCode || generateMonitoringCode(importItem)

return importItem
Expand Down

0 comments on commit eab69c8

Please sign in to comment.