Skip to content

Commit

Permalink
Use parameters for offer limit/sorting in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
ttoino committed Mar 7, 2023
1 parent a1f2a45 commit c674109
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/api/routes/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import CompanyService from "../../services/company.js";
import { ErrorTypes } from "../middleware/errorHandler.js";
import ValidationReasons from "../middleware/validators/validationReasons.js";
import { concurrentOffersNotExceeded } from "../middleware/validators/validatorUtils.js";
import CompanyConstants from "../../models/constants/Company.js";

import { or } from "../middleware/utils.js";

Expand Down Expand Up @@ -87,11 +86,7 @@ export default (app) => {
const offers = await new OfferService().getOffersByCompanyId(
req.params.companyId,
req.targetOwner,
req.hasAdminPrivileges,
{
sort: { publishDate: "desc" },
limit: CompanyConstants.offers.max_profile_visible,
}
req.hasAdminPrivileges
);
return res.json({ company, offers });
}
Expand Down
27 changes: 20 additions & 7 deletions src/services/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Account from "../models/Account.js";
import EmailService from "../lib/emailService.js";
import { OFFER_DISABLED_NOTIFICATION } from "../email-templates/companyOfferDisabled.js";
import OfferConstants from "../models/constants/Offer.js";
import CompanyConstants from "../models/constants/Company.js";
import base64url from "base64url";

const { ObjectId } = mongoose.Types;
Expand Down Expand Up @@ -376,7 +377,7 @@ class OfferService {

/**
* Checks whether a given offer is visible to a specific userCompanyId.
* Unpublished/Unactive offers may still be visible
* Unpublished/inactive offers may still be visible
* @param {*} offer
* @param {*} hasAdminPrivileges
* @param {*} userCompanyId
Expand All @@ -400,17 +401,29 @@ class OfferService {

/**
* Gets all the offers from a specific company that are visible to a specific user
* Note: This function will show even unpublished/unactive offers
* Note: This function will show even unpublished/inactive offers
* @param {*} companyId
* @param {*} userCompanyId
* @param {*} hasAdminPrivileges
* @param {*} limit
* @param {*} sortByPublishDate
* @returns Visible offers
*/
async getOffersByCompanyId(companyId, userCompanyId, hasAdminPrivileges, filters = {}) {
return (await Offer.find({ owner: companyId }, null, filters))
.filter((offer) =>
this.isVisibleOffer(offer, hasAdminPrivileges, userCompanyId)
);
async getOffersByCompanyId(
companyId,
userCompanyId,
hasAdminPrivileges,
limit = CompanyConstants.offers.max_profile_visible,
sortByPublishDate = true
) {
return (
await Offer.find({ owner: companyId }, null, {
limit,
sort: (sortByPublishDate && { publishDate: "desc" }) || undefined,
})
).filter((offer) =>
this.isVisibleOffer(offer, hasAdminPrivileges, userCompanyId)
);
}

async sendOfferDisabledNotification(offerId) {
Expand Down

0 comments on commit c674109

Please sign in to comment.