Skip to content

Commit

Permalink
fix: null or duplicate department
Browse files Browse the repository at this point in the history
  • Loading branch information
BatLeDev committed Feb 10, 2025
1 parent 4b58536 commit 0fa1919
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 32 deletions.
96 changes: 71 additions & 25 deletions api/src/routers/processings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ router.get('', async (req, res) => {
// Filter by owners
const owners = params.ownersFilter ? params.ownersFilter.split(',') : []
if (owners.length > 0) {
queryWithFilters.$or = [
{ 'owner.id': { $in: owners } },
{ 'owner.department': { $in: owners } }
]
queryWithFilters.$or = owners.map(ownerStr => {
const [type, id, department] = ownerStr.split(':')
if (department) return { 'owner.type': type, 'owner.id': id, 'owner.department': department }
else return { 'owner.type': type, 'owner.id': id }
})
}

// Get the processings
const [results, count] = await Promise.all([
size > 0 ? mongo.processings.find(queryWithFilters).limit(size).skip(skip).sort(sort).project(project).toArray() : Promise.resolve([]),
mongo.processings.countDocuments(query)
])
console.log('results', results)

const aggregationPipeline = [
{ $match: query },
Expand Down Expand Up @@ -151,7 +151,7 @@ router.get('', async (req, res) => {
{ $arrayToObject: { $map: { input: '$otherStatuses', as: 'el', in: { k: '$$el._id', v: '$$el.count' } } } }
]
},
plugins: { $arrayToObject: { $map: { input: '$plugins', as: 'el', in: { k: '$$el._id', v: '$$el.count' } } } },
plugins: { $arrayToObject: { $map: { input: '$plugins', as: 'el', in: { k: '$$el._id', v: '$$el.count' } } } }
}
}
}
Expand All @@ -163,34 +163,80 @@ router.get('', async (req, res) => {
{
$group: {
_id: {
$cond: {
if: { $gt: [{ $ifNull: ['$owner.department', null] }, null] }, // Si owner.department existe
then: '$owner.department', // On prend department
else: '$owner.id' // Sinon, on prend id
}
type: '$owner.type',
id: '$owner.id',
name: '$owner.name',
department: { $ifNull: ['$owner.department', 'null'] },
departmentName: { $ifNull: ['$owner.departmentName', 'null'] }
},
name: { $first: { $cond: { if: { $gt: [{ $ifNull: ['$owner.department', null] }, null] }, then: '$owner.departmentName', else: '$owner.name' } } },
count: { $sum: 1 }
}
}
]

aggregationPipeline[2].$replaceRoot.newRoot.owners = {
$arrayToObject: {
$map: {
input: '$owners',
as: 'el',
in: {
k: '$$el._id',
v: { name: '$$el.name', count: '$$el.count' }
},
{
$project: {
count: 1,
value: {
type: '$_id.type',
id: '$_id.id',
name: '$_id.name',
department: {
$cond: {
if: { $eq: ['$_id.department', 'null'] },
then: null,
else: '$_id.department'
}
},
departmentName: {
$cond: {
if: { $eq: ['$_id.department', 'null'] },
then: null,
else: '$_id.departmentName'
}
}
}
}
},
{
$group: {
_id: {
type: '$_id.type',
id: '$_id.id',
name: '$_id.name'
},
totalCount: { $sum: '$count' },
departments: {
$push: {
department: '$value.department',
departmentName: '$value.departmentName',
count: '$count'
}
}
}
},
{
$project: {
_id: 0,
type: '$_id.type',
id: '$_id.id',
name: '$_id.name',
totalCount: 1,
departments: {
$filter: {
input: '$departments',
as: 'dept',
cond: { $ne: ['$$dept.department', null] } // Filtrer les départements null
}
}
}
}
}
]

// Ajout des `owners` dans le résultat final
aggregationPipeline[2].$replaceRoot.newRoot.owners = { $ifNull: ['$owners', []] }
}

const aggregationResult = await mongo.processings.aggregate(aggregationPipeline).toArray()
const facets = aggregationResult[0] || { statuses: {}, plugins: {}, owners: {} }
const facets = aggregationResult[0] || { statuses: {}, plugins: {}, owners: [] }

res.json({ results: results.map((p) => cleanProcessing(p as Processing, sessionState)), facets, count })
})
Expand Down
26 changes: 20 additions & 6 deletions ui/src/components/processings-actions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
hide-details
class="mt-2 mx-4 text-admin"
/>
<v-select
<v-autocomplete
v-if="showAll"
v-model="ownersSelected"
:items="ownersItems"
Expand All @@ -168,7 +168,7 @@ import OwnerPick from '@data-fair/lib-vuetify/owner-pick.vue'
const processingsProps = defineProps<{
adminMode: boolean,
ownerFilter: string,
facets: { statuses: Record<string, number>, plugins: Record<string, number>, owners: Record<string, { name: string, count: number }> },
facets: { statuses: Record<string, number>, plugins: Record<string, number>, owners: { id: string, name: string, totalCount: number, type: string, departments: { department: string, departmentName: string, count: number }[] }[] },
isSmall: boolean,
processings: any[]
}>()
Expand Down Expand Up @@ -243,11 +243,25 @@ const ownersItems = computed(() => {
if (!processingsProps.facets.owners) return []
return Object.entries(processingsProps.facets.owners)
.map(([ownerKey, owner]) => {
return {
display: `${owner.name} (${owner.count})`,
ownerKey
.flatMap(([, owner]) => {
const items = []
// Si l'organisation a des départements
if (owner.departments.length > 0) {
owner.departments.forEach(department => {
// Ajout d'un élément pour chaque département
items.push({
display: `${owner.name} - ${department.departmentName} (${department.count})`,
ownerKey: `organization:${owner.id}:${department.department}`
})
})
}
items.push({
display: `${owner.name} (${owner.totalCount})`,
ownerKey: `${owner.type}:${owner.id}`
})
return items
})
.sort((a, b) => a.display.localeCompare(b.display))
})
Expand Down
2 changes: 1 addition & 1 deletion ui/src/pages/processings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const processingsParams = computed(() => {
const processingsFetch = useFetch<{
results: Processing[],
facets: { statuses: Record<string, number>, plugins: Record<string, number>, owners: Record<string, { name: string, count: number }> },
facets: { statuses: Record<string, number>, plugins: Record<string, number>, owners: { id: string, name: string, totalCount: number, type: string, departments: { department: string, departmentName: string, count: number }[] }[] },
count: number
}>(`${$apiPath}/processings`, { query: processingsParams })
Expand Down

0 comments on commit 0fa1919

Please sign in to comment.