findByOrganizationId also returns tags

This commit is contained in:
xfarrow 2024-03-27 15:26:15 +01:00
parent 21d32a89f9
commit e21550f803
1 changed files with 19 additions and 3 deletions

View File

@ -72,10 +72,26 @@ async function findById(jobOfferId) {
}).select().first(); }).select().first();
} }
async function findByOrganizationId(organizationId){ /**
* Get all job offers for a specific organization, including their tags
* @param {*} organizationId
* @returns
*/
async function findByOrganizationId(organizationId) {
// This is equal to
//
// select "JobOffer".*, json_agg("Tag".tag) as tags
// from "JobOffer"
// left join "JobOfferTag" on "JobOffer".id = "JobOfferTag".job_offer_id
// left join "Tag" on "JobOfferTag".tag_id = "Tag".id
// where "JobOffer".organization_id = organizationId
// GROUP BY "JobOffer".id
const result = await knex('JobOffer') const result = await knex('JobOffer')
.where({organization_id: organizationId}) .select('JobOffer.*', knex.raw('json_agg("Tag".tag) as tags'))
.select(); .leftJoin('JobOfferTag', 'JobOffer.id', 'JobOfferTag.job_offer_id')
.leftJoin('Tag', 'JobOfferTag.tag_id', 'Tag.id')
.where('JobOffer.organization_id', organizationId)
.groupBy('JobOffer.id');
return result; return result;
} }