Skip to content

Commit

Permalink
fix myVisit
Browse files Browse the repository at this point in the history
le visite di una persona vengono prese tramite associazione referencePeople invece che tramite owner
  • Loading branch information
paolini committed Jan 9, 2024
1 parent 46476de commit 64b552f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
13 changes: 9 additions & 4 deletions env.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Rename this file to .env
ADMIN_USER=admin
ADMIN_PASSWORD=secret
OAUTH2_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxx
OAUTH2_CLIENT_ID=yyyyyyyyyyyyyyyyyyyyyyyy
OAUTH2_USERNAME_FIELD=email
CORS_ORIGIN=http://localhost:3000
# SESSION_SECRET: "1234567890"
# JWT_SECRET: "1234567890"
# OAUTH2_CLIENT_ID: 1234567890
# OAUTH2_AUTHORIZE_URL: "https://iam.unipi.it/oauth2/authorize"
# OAUTH2_TOKEN_URL: "https://iam.unipi.it/oauth2/token"
# OAUTH2_USERINFO_URL: "https://iam.unipi.it/oauth2/userinfo"
# OAUTH2_LOGOUT_URL: "https://iam.unipi.it/oidc/logout"
# OAUTH2_CLIENT_SECRET: "1234567890"
# OAUTH2_USERNAME_FIELD: "email"
55 changes: 49 additions & 6 deletions server/controllers/processes/visitsMy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert')
const { ObjectId } = require('mongoose').Types

const Visit = require('../../models/Visit')
const Person = require('../../models/Person')
const { log } = require('../middleware')

const router = express.Router()
Expand All @@ -22,18 +23,33 @@ function pastDate() {
return d
}

router.get('/', async (req, res) => {
async function getPersonByEmail(email) {
const persons = await Person.aggregate([
{ $match: {$or: [{email}, {alternativeEmails: email}]}},
])
// console.log(`lookup person with email ${email}: ${JSON.stringify(persons)}`)
if (persons.length === 0) return null
if (persons.length > 0) {
console.log(`WARNING: found ${persons.length} persons with email ${email}`)
}
return persons[0]
}

router.get('/', async (req, res) => {
if (!req.user) {
res.status(401).json({
result: "Unauthorized"
})
return
}

if (!req.user.email) return res.json({ data: [], note: `user ${req.user._id} has no email`})
const person = await getPersonByEmail(req.user.email)
if (!person) return res.json({ data: [], note: `no person found matching user ${req.user._id} email`})

const data = await Visit.aggregate([
{ $match: {
createdBy: req.user._id,
referencePeople: person._id,
endDate: { $gte: pastDate() },
}},
{ $lookup: {
Expand All @@ -58,24 +74,38 @@ router.get('/', async (req, res) => {
}},
]
}},
{ $unwind: {
path: '$person',
preserveNullAndEmptyArrays: true,
}},
])

res.json({ data, DAYS_BACK })
res.json({ data, person, DAYS_BACK })
})

router.delete('/:id', async (req, res) => {
assert(req.user._id)

if (!req.user.email) return res.status(404).json({ error: `user ${req.user._id} has no email`})
const person = await getPersonByEmail(req.user.email)
if (!person) return res.status(404).json({ error: `no person found matching user ${req.user._id} email`})

const visit = await Visit.findOneAndDelete({
_id: new ObjectId(req.params.id),
createdBy: req.user._id,
referencePeople: person._id,
endDate: { $gte: pastDate() },
})

log(req, visit, {})
res.json({})
})

router.get('/:id', async (req, res) => {
assert(req.user._id)
if (!req.user.email) return res.status(404).json({ error: `user ${req.user._id} has no email`})
const person = await getPersonByEmail(req.user.email)
if (!person) return res.status(404).json({ error: `no person found matching user ${req.user._id} email`})

if (req.params.id === '__new__') {
// return empty object
const visit = new Visit().toObject()
Expand All @@ -91,7 +121,7 @@ router.get('/:id', async (req, res) => {
const data = await Visit.aggregate([
{ $match: {
_id,
createdBy: req.user._id,
referencePeople: person._id,
endDate: { $gte: pastDate() },
}},
{ $lookup: {
Expand Down Expand Up @@ -203,15 +233,21 @@ router.get('/:id', async (req, res) => {
res.status(404).json({ error: "Not found" })
return
}
res.json(data[0])
res.json({...data[0], user_person: person})
})

router.put('/', async (req, res) => {
const payload = {...req.body}

assert(req.user._id)
if (!req.user.email) return res.status(404).json({ error: `user ${req.user._id} has no email`})
const person = await getPersonByEmail(req.user.email)
if (!person) return res.status(404).json({ error: `no person found matching user ${req.user._id} email`})

// override fields that user cannot change
payload.createdBy = req.user._id
payload.updatedBy = req.user._id
payload.referencePeople = [person._id]
delete payload._id

if (!payload.endDate || new Date(payload.endDate) < pastDate()) {
Expand All @@ -229,14 +265,21 @@ router.put('/', async (req, res) => {
router.patch('/:id', async (req, res) => {
const payload = {...req.body}

assert(req.user._id)
if (!req.user.email) return res.status(404).json({ error: `user ${req.user._id} has no email`})
const person = await getPersonByEmail(req.user.email)
if (!person) return res.status(404).json({ error: `no person found matching user ${req.user._id} email`})

// remove fields that user cannot change
delete payload._id
delete payload.createdBy
delete payload.referencePeople
payload.updatedBy = req.user._id

const visit = await Visit.findOneAndUpdate(
{ _id: new ObjectId(req.params.id),
createdBy: req.user._id,
referencePeople: [person._id],
endDate: { $gte: pastDate() }},
payload)

Expand Down
2 changes: 1 addition & 1 deletion src/processes/Visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function Visit({variant}) {
const path = `process/${variant||''}visits/${id || '__new__'}`
const query = useQuery(path.split('/'))
if (query.isLoading) return <Loading />
if (query.isError) return <div>Errore caricamento {`${query.error}`}</div>
if (query.isError) return <div>Errore caricamento: {query.error.response.data?.error || `${query.error}`}</div>

return <VisitForm visit={query.data} variant={variant||''}/>
}
Expand Down

0 comments on commit 64b552f

Please sign in to comment.