Skip to content

Commit

Permalink
possibilità di modificare l'owner se undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Nov 14, 2023
1 parent d0c10d0 commit 50efdd1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-manager",
"version": "1.3.12",
"version": "1.3.13",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.1.2",
Expand Down
11 changes: 10 additions & 1 deletion server/controllers/Controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { ObjectId } = require('mongoose').Types

const User = require('../models/User')
const {log, requireSomeRole, requireUser, allowAnonymous} = require('./middleware')

function sendBadRequest(res, message) {
Expand Down Expand Up @@ -541,6 +543,12 @@ class Controller {
try {
const was = await this.Model.findById(id)
await log(req, was, payload)
if (was.createdBy !== undefined) delete payload.createdBy
if (payload.createdBy) {
const user = await User.findOne({username: payload.createdBy})
if (!user) throw new Error(`user ${payload.createdBy} not found`)
payload.createdBy = user._id
}
was.set({...was, ...payload})
await was.save()
res.send(was)
Expand Down Expand Up @@ -612,7 +620,8 @@ class Controller {
const payload = {...req.body,
updatedBy: req.user._id
}
delete payload.createdBy
// è possibile modificare l'owner se undefined
//delete payload.createdBy
delete payload.createdAt
delete payload.updatedAt
this.patch(req, res, req.params.id, payload)
Expand Down
25 changes: 21 additions & 4 deletions src/components/ModelEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Form, Button, ButtonGroup } from 'react-bootstrap'
import { Card } from 'react-bootstrap'

import { useEngine } from '../Engine'
import { ModelInputs } from '../components/ModelInput'
import { ModelInput, ModelInputs } from '../components/ModelInput'
import Timestamps from '../components/Timestamps'
import Loading from '../components/Loading'

Expand Down Expand Up @@ -56,8 +56,9 @@ export default function ModelEdit({Model, id, clone_id, onSave, onCancel, onDele
return v1 === v2
}

const modifiedFields = Object.keys(modifiedObj)
const modifiedFields = [...Object.keys(modifiedObj)]
.filter(key => !compareValue(modifiedObj[key], originalObj[key]))
console.log(`Modified fields: ${JSON.stringify(modifiedFields)}`)

const changed = modifiedFields.length > 0

Expand Down Expand Up @@ -105,6 +106,22 @@ export default function ModelEdit({Model, id, clone_id, onSave, onCancel, onDele
modifiedFields={modifiedFields}
onChange={onChange && onChange(setModifiedObj)}
/>
{ /* input speciale per modificare il campo createdBy */
originalObj.createdBy === undefined && !create &&
<ModelInput
schema={{
label: 'Creato da',
...Model.schema.fields.createdBy}}
setValue={(value) => {
setModifiedObj(obj => ({...obj, createdBy: value}))
}}
edit={true}
value={modifiedObj.createdBy}
obj={modifiedObj}
setObj={setModifiedObj}
modified={modifiedFields.includes("createdBy")}
onChange={onChange && onChange(setModifiedObj)}
/>}
<ButtonGroup className="mt-3">
<Button
onClick={ submit }
Expand All @@ -121,12 +138,12 @@ export default function ModelEdit({Model, id, clone_id, onSave, onCancel, onDele
onClick={ () => deleteObj(modifiedObj) }
className="btn btn-danger pull-right">
elimina {objName}
</Button>}
</Button>}
</ButtonGroup>
</Form>
</Card.Body>
<Card.Footer>
<Timestamps obj={originalObj} />
<Timestamps obj={originalObj}/>
</Card.Footer>
</Card>
}
18 changes: 10 additions & 8 deletions src/components/ModelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ export function ModelFieldInput({ id, schema, value, setValue, api_prefix }) {
if (schema.items['x-ref'] === 'Institution') return element(InstitutionInput, {multiple:true, api_prefix: api_prefix})
return <p>x-ref to {schema.items['x-ref']} not yet implemented in array</p>
} else {
if (schema['x-ref'] === 'Person') return element(PersonInput, {api_prefix: api_prefix})
if (schema['x-ref'] === 'Room') return element(RoomInput, {api_prefix: api_prefix})
if (schema['x-ref'] === 'ConferenceRoom') return element(ConferenceRoomInput, {api_prefix: api_prefix})
if (schema['x-ref'] === 'SeminarCategory') return element(SeminarCategoryInput, {api_prefix: api_prefix})
if (schema['x-ref'] === 'Institution') return element(InstitutionInput, {api_prefix: api_prefix})
if (schema['x-ref']) return <p>x-ref to {schema['x-ref']} not yet implemented</p>
const xref = schema['x-ref']
if (xref === 'Person') return element(PersonInput, {api_prefix: api_prefix})
if (xref === 'Room') return element(RoomInput, {api_prefix: api_prefix})
if (xref === 'ConferenceRoom') return element(ConferenceRoomInput, {api_prefix: api_prefix})
if (xref === 'SeminarCategory') return element(SeminarCategoryInput, {api_prefix: api_prefix})
if (xref === 'Institution') return element(InstitutionInput, {api_prefix: api_prefix})
if (xref === 'User') return element(StringInput, {api_prefix: api_prefix})
if (xref) return <p>x-ref to {xref} not yet implemented</p>
if (schema.format === 'date-time') {
if (schema.widget === 'datetime') return element(DatetimeInput) // use formatted text input "YYYY-MM-DD HH:mm"
return element(DateInput) // use date only widget
Expand All @@ -84,7 +86,7 @@ export function ModelFieldInput({ id, schema, value, setValue, api_prefix }) {

export function ModelInput({ field, modified, schema, value, setValue, api_prefix}) {
const id = useId()
const label = schema.items?.label || schema.label || field
const label = schema?.items?.label || schema?.label || field

return <Form.Group className="row my-2">
<Form.Label className={ "col-form-label text-end col-sm-2 " + (modified ? "bg-warning" : "") } htmlFor={ id }>
Expand All @@ -100,7 +102,7 @@ export function ModelInput({ field, modified, schema, value, setValue, api_prefi
/>
</div>
<div className="col-sm-2"></div>
<div className="col-sm-10 form-text">{schema.help}</div>
<div className="col-sm-10 form-text">{schema?.help}</div>
</Form.Group>
}

Expand Down

0 comments on commit 50efdd1

Please sign in to comment.