Skip to content

Commit

Permalink
removed offer grace period
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosMealha committed Feb 19, 2022
1 parent d4dfefa commit c191ea8
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 61 deletions.
11 changes: 0 additions & 11 deletions src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import OfferConstants from "../../../models/constants/Offer.js";
import Company from "../../../models/Company.js";
import Offer, { validatePublishEndDateLimit } from "../../../models/Offer.js";
import {
HOUR_IN_MS,
OFFER_EDIT_GRACE_PERIOD_HOURS,
MONTH_IN_MS,
OFFER_MAX_LIFETIME_MONTHS
} from "../../../models/constants/TimeConstants.js";
Expand Down Expand Up @@ -430,17 +428,8 @@ export const isEditable = async (req, res, next) => {
const offer = await Offer.findById(req.params.offerId);
const currentDate = new Date(Date.now());

// Verify if offer editing grace period is over
const timeDiff = currentDate - offer.createdAt;
const diffInHours = timeDiff / HOUR_IN_MS;

if (offer.publishEndDate.toISOString() <= currentDate.toISOString()) {
return next(new APIError(HTTPStatus.FORBIDDEN, ErrorTypes.FORBIDDEN, ValidationReasons.OFFER_EXPIRED(req.params.offerId)));
} else if (offer.publishDate.toISOString() <= currentDate.toISOString() && diffInHours > OFFER_EDIT_GRACE_PERIOD_HOURS) {
return next(
new APIError(HTTPStatus.FORBIDDEN, ErrorTypes.FORBIDDEN, ValidationReasons.OFFER_EDIT_PERIOD_OVER(diffInHours.toFixed(2)))
);

}

return next();
Expand Down
1 change: 0 additions & 1 deletion src/models/constants/TimeConstants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const MONTH_IN_MS = 1000 * 3600 * 24 * 30.42;
export const HOUR_IN_MS = 1000 * 3600;
export const OFFER_MAX_LIFETIME_MONTHS = 6;
export const OFFER_EDIT_GRACE_PERIOD_HOURS = 1;
49 changes: 0 additions & 49 deletions test/end-to-end/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import ValidationReasons from "../../src/api/middleware/validators/validationRea
import { Types } from "mongoose";
import CompanyConstants from "../../src/models/constants/Company";
import {
OFFER_EDIT_GRACE_PERIOD_HOURS,
HOUR_IN_MS,
MONTH_IN_MS,
OFFER_MAX_LIFETIME_MONTHS
} from "../../src/models/constants/TimeConstants";
Expand Down Expand Up @@ -1840,8 +1838,6 @@ describe("Offer endpoint tests", () => {
describe("POST /offers/edit/:offerId", () => {
let createOffer,
expired_test_offer,
grace_period_over_test_offer,
grace_period_valid_test_offer,
future_test_offer,
valid_test_offer_1,
valid_test_offer_2,
Expand Down Expand Up @@ -1874,18 +1870,6 @@ describe("Offer endpoint tests", () => {
"publishEndDate": "2019-11-18"
}));

grace_period_over_test_offer = await createOffer(generateTestOffer({
"publishDate": (new Date(Date.now())).toISOString(),
"publishEndDate": (new Date(Date.now() + (2 * DAY_TO_MS))).toISOString()
}
));

grace_period_valid_test_offer = await createOffer(generateTestOffer({
"publishDate": (new Date(Date.now())).toISOString(),
"publishEndDate": (new Date(Date.now() + (2 * DAY_TO_MS))).toISOString()
}
));

future_test_offer = await createOffer(generateTestOffer({
"publishDate": (new Date(Date.now() + (2 * DAY_TO_MS))).toISOString(),
"publishEndDate": (new Date(Date.now() + (3 * DAY_TO_MS))).toISOString(),
Expand Down Expand Up @@ -1957,39 +1941,6 @@ describe("Offer endpoint tests", () => {
expect(res.body.errors).toContainEqual({ msg: ValidationReasons.OFFER_EXPIRED(expired_test_offer._id.toString()) });
});

describe("should fail if offer with grace period over", () => {
const RealDateNow = Date.now;
const mockDate = new Date(Date.now() + (OFFER_EDIT_GRACE_PERIOD_HOURS * HOUR_IN_MS * 2));
beforeEach(() => {
Date.now = () => mockDate.getTime();
});

afterEach(() => {
Date.now = RealDateNow;
});

test("should fail offer with grace period over", async () => {
const expired_over_hours =
((new Date(Date.now())).getTime() - (new Date(grace_period_over_test_offer.createdAt)).getTime()) / HOUR_IN_MS;
const res = await test_agent
.post(`/offers/edit/${grace_period_over_test_offer._id.toString()}`)
.send(withGodToken())
.expect(HTTPStatus.FORBIDDEN);
expect(res.body).toHaveProperty("errors");
expect(res.body.errors).toContainEqual({
msg: ValidationReasons.OFFER_EDIT_PERIOD_OVER(expired_over_hours.toFixed(2))
});
});

});

test("should allow editing offer with valid grace period", async () => {
await test_agent
.post(`/offers/edit/${grace_period_valid_test_offer._id.toString()}`)
.send(withGodToken())
.expect(HTTPStatus.OK);
});

test("should allow editing offer in the future", async () => {
await test_agent
.post(`/offers/edit/${future_test_offer._id.toString()}`)
Expand Down

0 comments on commit c191ea8

Please sign in to comment.