Skip to content

Commit

Permalink
fix: part time care taker availability bids constraint (#35)
Browse files Browse the repository at this point in the history
- fixed the part timer care taker avail check constraint for bids, made sure that it checks the rows involving the care taker.
- fixed fetch expected salary api by adjusting month values
  • Loading branch information
ambhinav authored Nov 5, 2020
1 parent 704804a commit 163728d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions setup/CreateDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ $$
SELECT 1
FROM care_taker_full_timers_unavailable_dates
WHERE date BETWEEN start_date::date AND end_date::date
AND email = new_care_taker_email
)
THEN
RETURN false;
Expand All @@ -441,6 +442,7 @@ $$
SELECT count(*)
FROM care_taker_part_timers_available_dates
WHERE date BETWEEN start_date::date AND end_date::date -- dates inclusive of start_date and end_date
AND email = new_care_taker_email
) = (calculate_duration(start_date, end_date)) -- offset of +1 to include start_date
THEN
RETURN true;
Expand Down
4 changes: 2 additions & 2 deletions setup/SeedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ const main = async () => {
}),
);
await Promise.all(
_.map(careTakers, async ({email}) => {
await careTakerEmploymentTypePayload(client, email);
_.map(careTakers, async ({email}, i) => {
await careTakerEmploymentTypePayload(client, email, i);
}),
);
await client3.query('COMMIT');
Expand Down
2 changes: 1 addition & 1 deletion src/Pet/PetService.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const FetchExpectedSalary = async ({email}) => {
0,
).getDate();

const paddedMonth = `000${now.getMonth().toString()}`;
const paddedMonth = `000${(now.getMonth() + 1).toString()}`;
const newMonth = paddedMonth.substr(paddedMonth.length - 2);
const dateFormat = `${now.getFullYear().toString()}-${newMonth}-%`;
const caretakerType = await pool.query(SQLQueries.FETCH_CARE_TAKER_ROLE, [
Expand Down
40 changes: 33 additions & 7 deletions tests/Bid/BidCreateService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Test BidCreate Service', () => {
await pool.query('DELETE FROM pets');
await pool.query('DELETE FROM pet_categories');
await UserFixtures.SeedPetOwners(1);
await UserFixtures.SeedCareTakers(2);
await UserFixtures.SeedCareTakers(3);
await UserFixtures.SeedCareTakerRole({
email: 'test0@example.com',
role: RoleUtils.CARE_TAKER_FULL_TIMER,
Expand All @@ -31,6 +31,10 @@ describe('Test BidCreate Service', () => {
email: 'test1@example.com',
role: RoleUtils.CARE_TAKER_PART_TIMER,
});
await UserFixtures.SeedCareTakerRole({
email: 'test2@example.com',
role: RoleUtils.CARE_TAKER_PART_TIMER,
});
await PetFixtures.SeedPetCategories(2);
const email = 'test0@example.com';
const category = 'category0';
Expand Down Expand Up @@ -86,7 +90,7 @@ describe('Test BidCreate Service', () => {
const petOwnerEmail = 'test0@example.com';
const petName = 'pet0';
const {startDate, endDate} = BidFixtures.CreateBidDates();
await DateFixtures.SeedAvaliableDates(8);
await DateFixtures.SeedAvaliableDates(8, careTakerEmail);
await BidService.BidCreate({
petName,
petOwnerEmail,
Expand Down Expand Up @@ -351,7 +355,7 @@ describe('Test BidCreate Service', () => {

it('Service should not automatically accept bid for part timers', async () => {
const careTakerEmail = 'test1@example.com';
await DateFixtures.SeedAvaliableDates(8);
await DateFixtures.SeedAvaliableDates(8, careTakerEmail);
await pool.query(`
INSERT INTO care_taker_skills(email, category, price)
VALUES('test0@example.com', 'category0', 10);
Expand Down Expand Up @@ -382,7 +386,7 @@ describe('Test BidCreate Service', () => {
it('Service should not accept bid if part timer has more than 5 pets and is above 4 stars', async () => {
await pool.query('DELETE FROM pets');
const careTakerEmail = 'test1@example.com';
await DateFixtures.SeedAvaliableDates(8);
await DateFixtures.SeedAvaliableDates(8, careTakerEmail);
const petOwnerEmail = 'test0@example.com';
const petNames = ['pet0', 'pet1', 'pet2', 'pet3', 'pet4'];
const category = 'category0';
Expand Down Expand Up @@ -429,7 +433,7 @@ describe('Test BidCreate Service', () => {
it('Service should accept bid if part timer has 5 pets or less, more than 2 pets and is above 4 stars', async () => {
await pool.query('DELETE FROM pets');
const careTakerEmail = 'test1@example.com';
await DateFixtures.SeedAvaliableDates(8);
await DateFixtures.SeedAvaliableDates(8, careTakerEmail);
const petOwnerEmail = 'test0@example.com';
const petNames = ['pet0', 'pet1', 'pet2'];
const category = 'category0';
Expand Down Expand Up @@ -495,7 +499,7 @@ describe('Test BidCreate Service', () => {
it('Service should not accept bid if part timer has 5 pets or less, more than 2 pets and is below 4 stars', async () => {
await pool.query('DELETE FROM pets');
const careTakerEmail = 'test1@example.com';
await DateFixtures.SeedAvaliableDates(8);
await DateFixtures.SeedAvaliableDates(8, careTakerEmail);
const petOwnerEmail = 'test0@example.com';
const petNames = ['pet0', 'pet1'];
const category = 'category0';
Expand Down Expand Up @@ -693,7 +697,29 @@ describe('Test BidCreate Service', () => {
const petName = 'pet0';
// start date is today and end date is 7 days from now
const {startDate, endDate} = BidFixtures.CreateBidDates();
await DateFixtures.SeedAvaliableDates(7); // one day missing
await DateFixtures.SeedAvaliableDates(7, parTimeCareTakerEmail); // one day missing

await Assert.rejects(
() =>
BidService.BidCreate({
petName,
petOwnerEmail,
careTakerEmail: parTimeCareTakerEmail,
startDate,
endDate,
}),
Error,
);
});

it('Service should reject a bid if target part time care taker is not available for the bid dates, but another one is', async () => {
const parTimeCareTakerEmail = 'test1@example.com';
const petOwnerEmail = 'test0@example.com';
const petName = 'pet0';
// start date is today and end date is 7 days from now
const {startDate, endDate} = BidFixtures.CreateBidDates();
await DateFixtures.SeedAvaliableDates(7, parTimeCareTakerEmail); // target is unavail
await DateFixtures.SeedAvaliableDates(8, 'test2@example.com'); // another pt is

await Assert.rejects(
() =>
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/DateFixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const SeedAvailableDate = async ({email, date}) => {
};

/** Seeds available dates for a part time care taker that match those created in @see BidFixtures.CreateBidDates */
const SeedAvaliableDates = async (i) => {
const email = 'test1@example.com';
const SeedAvaliableDates = async (i, email) => {
await Promise.all(
_.times(i, (idx) => {
const date = moment().add(idx, 'days').toISOString(true);
Expand Down

0 comments on commit 163728d

Please sign in to comment.