Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: project query improvements #1822

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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