Skip to content

Commit

Permalink
Set isPending of all company's offers to false when its application i…
Browse files Browse the repository at this point in the history
…s approved

Co-authored-by: Francisco Cardoso <[email protected]>
  • Loading branch information
dsantosferreira and FranciscoCardoso913 committed Apr 16, 2023
1 parent ab43bd3 commit 0d10252
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/routes/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default (app) => {
const params = {
...req.body,
owner: req.targetOwner,
isPending: application.state === ApplicationStatus.APPROVED ? false : true,
isPending: !(application.state === ApplicationStatus.APPROVED),
};

const offer = await (new OfferService()).create(params);
Expand Down
9 changes: 6 additions & 3 deletions src/api/routes/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ApplicationService, {
} from "../../services/application.js";

import { buildErrorResponse, ErrorTypes } from "../middleware/errorHandler.js";
import Company from "../../models/Company.js";
import CompanyService from "../../services/company.js";

const router = Router();

Expand Down Expand Up @@ -82,9 +84,10 @@ export default (app) => {
async (req, res, next) => {

try {
/* TODO: check return account logic*/
const { account } = await (new ApplicationService()).approve(req.params.id);
return res.json(account);
const account = await (new ApplicationService()).approve(req.params.id);
const company = await Company.findOne({ company: account.company });
await (new CompanyService()).releaseOffers(company);
return res.json({account});
} catch (err) {
console.error(err);
if (err instanceof CompanyApplicationNotFound) {
Expand Down
6 changes: 2 additions & 4 deletions src/services/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
REJECTION_NOTIFICATION,
} from "../email-templates/companyApplicationApproval.js";
import config from "../config/env.js";
import Account from "../models/constants/Account.js";
import Account from "../models/Account.js";


export class CompanyApplicationNotFound extends Error {
Expand Down Expand Up @@ -183,9 +183,7 @@ class CompanyApplicationService {
console.error(e);
throw new CompanyApplicationAlreadyReviewed(CompanyApplicationRules.CANNOT_REVIEW_TWICE.msg);
}
return Account.findOne({ email: application.email });

/* TODO: Make offers unpending */
return Account.findOne({ email: application.email });
}

async reject(id, reason, options) {
Expand Down
10 changes: 10 additions & 0 deletions src/services/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { COMPANY_BLOCKED_NOTIFICATION,
import EmailService from "../lib/emailService.js";
import Account from "../models/Account.js";
import Company from "../models/Company.js";
import Offer from "../models/Offer.js"

class CompanyService {
getOffersInTimePeriod(owner, publishDate, publishEndDate, OfferModel) {
return OfferModel.find({
Expand Down Expand Up @@ -189,6 +191,14 @@ class CompanyService {
}
}

async releaseOffers(companyId) {
try {
await Offer.updateMany({ company : companyId }, { $set: { "isPending" : false } });
} catch (err) {
console.error(err);
throw err;
}
}
}

export default CompanyService;

0 comments on commit 0d10252

Please sign in to comment.