Skip to content

Commit

Permalink
fix: project query improvements (#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed May 30, 2024
1 parent fbd14cf commit 183dd60
Showing 1 changed file with 6 additions and 41 deletions.
47 changes: 6 additions & 41 deletions src/back/models/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,52 +46,17 @@ export default class ProjectModel extends Model<ProjectAttributes> {
SELECT
pr.*,
p.user as author,
COALESCE(json_agg(
json_build_object(
'id', pe.id,
'project_id', pe.project_id,
'address', pe.address,
'name', pe.name,
'role', pe.role,
'about', pe.about,
'relevantLink', pe."relevantLink",
'updated_by', pe.updated_by,
'updated_at', pe.updated_at,
'created_at', pe.created_at
) ORDER BY pe.created_at ASC) FILTER (WHERE pe.id IS NOT NULL), '[]') AS personnel,
COALESCE(json_agg(
json_build_object(
'id', mi.id,
'project_id', mi.project_id,
'title', mi.title,
'description', mi.description,
'delivery_date', mi.delivery_date,
'status', mi.status,
'updated_by', mi.updated_by,
'updated_at', mi.updated_at,
'created_by', mi.created_by,
'created_at', mi.created_at
) ORDER BY mi.created_at ASC) FILTER (WHERE mi.id IS NOT NULL), '[]') AS milestones,
COALESCE(json_agg(
json_build_object(
'id', link.id,
'project_id', link.project_id,
'label', link.label,
'url', link.url,
'updated_by', link.updated_by,
'updated_at', link.updated_at,
'created_by', link.created_by,
'created_at', link.created_at
) ORDER BY link.created_at ASC) FILTER (WHERE link.id IS NOT NULL), '[]') AS links,
COALESCE(json_agg(DISTINCT to_jsonb(pe.*)) FILTER (WHERE pe.id IS NOT NULL), '[]') as personnel,
COALESCE(json_agg(DISTINCT to_jsonb(mi.*)) FILTER (WHERE mi.id IS NOT NULL), '[]') as milestones,
COALESCE(json_agg(DISTINCT to_jsonb(li.*)) FILTER (WHERE li.id IS NOT NULL), '[]') as links,
COALESCE(array_agg(co.address) FILTER (WHERE co.address IS NOT NULL), '{}') AS coauthors
FROM ${table(ProjectModel)} pr
JOIN ${table(ProposalModel)} p ON pr.proposal_id = p.id
LEFT JOIN ${table(PersonnelModel)} pe ON pr.id = pe.project_id AND pe.deleted = false
LEFT JOIN ${table(ProjectMilestoneModel)} mi ON pr.id = mi.project_id
LEFT JOIN ${table(ProjectLinkModel)} link ON pr.id = link.project_id
LEFT JOIN ${table(CoauthorModel)} co ON pr.proposal_id = co.proposal_id AND co.status = ${
CoauthorStatus.APPROVED
}
LEFT JOIN ${table(ProjectLinkModel)} li ON pr.id = li.project_id
LEFT JOIN ${table(CoauthorModel)} co ON pr.proposal_id = co.proposal_id
AND co.status = ${CoauthorStatus.APPROVED}
WHERE pr.id = ${id}
GROUP BY pr.id, p.user;
`
Expand Down

0 comments on commit 183dd60

Please sign in to comment.