Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added(server/api): availability slots "find" and "set" api handlers. #278

Merged
merged 3 commits into from
Jan 15, 2025

Conversation

mmoehabb
Copy link
Collaborator

@mmoehabb mmoehabb commented Jan 6, 2025

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from d26c6df to 949ebfc Compare January 6, 2025 14:57
@mmoehabb mmoehabb self-assigned this Jan 6, 2025
@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch 3 times, most recently from 222472c to 1d5aa0a Compare January 6, 2025 15:31
@neuodev
Copy link
Member

neuodev commented Jan 6, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 6, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 6, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from 1d5aa0a to e19fdab Compare January 7, 2025 12:26
@neuodev
Copy link
Member

neuodev commented Jan 7, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 7, 2025

@mmoehabb please rebase and resolve the conflicts.

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from e19fdab to 7ec0498 Compare January 8, 2025 04:42
@neuodev
Copy link
Member

neuodev commented Jan 8, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from 7ec0498 to 447a967 Compare January 8, 2025 05:09
@neuodev
Copy link
Member

neuodev commented Jan 8, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from 447a967 to f23361f Compare January 8, 2025 20:25
@neuodev
Copy link
Member

neuodev commented Jan 8, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from f23361f to 1560efe Compare January 8, 2025 20:31
@neuodev
Copy link
Member

neuodev commented Jan 8, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb
Copy link
Collaborator Author

mmoehabb commented Jan 8, 2025

@neuodev
I have updated the delete mechanism of AvailabilitySlots table. Please re-review the pull request.

@neuodev
Copy link
Member

neuodev commented Jan 12, 2025

@mmoehabb please rebase and resolve the conflicts.

@neuodev
Copy link
Member

neuodev commented Jan 12, 2025

You are missing the deleted flag here in the update query.

packages/models/src/availabilitySlots.ts Outdated Show resolved Hide resolved
packages/models/src/availabilitySlots.ts Outdated Show resolved Hide resolved
packages/models/src/availabilitySlots.ts Outdated Show resolved Hide resolved
packages/models/src/availabilitySlots.ts Outdated Show resolved Hide resolved
packages/models/src/interviews.ts Outdated Show resolved Hide resolved
services/server/src/handlers/availabilitySlot.ts Outdated Show resolved Hide resolved
services/server/src/handlers/availabilitySlot.ts Outdated Show resolved Hide resolved
services/server/src/handlers/availabilitySlot.ts Outdated Show resolved Hide resolved
services/server/src/handlers/availabilitySlot.ts Outdated Show resolved Hide resolved
@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch 2 times, most recently from edee3ad to a4c9e4b Compare January 12, 2025 17:23
@neuodev
Copy link
Member

neuodev commented Jan 12, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from a4c9e4b to 1861159 Compare January 12, 2025 18:32
@neuodev
Copy link
Member

neuodev commented Jan 12, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch from 1861159 to 1906f49 Compare January 13, 2025 23:31
@neuodev
Copy link
Member

neuodev commented Jan 13, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb requested a review from neuodev January 13, 2025 23:36
@@ -151,6 +157,24 @@ export class Lessons {
.where(this.columns.lessons("id"), id);
}

async cancelBatch({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the old one should be updated to accept a list of id not just one @mmoehabb

import { dayjs } from "@/dayjs";
import { flatten, orderBy } from "lodash";
import { INTERVIEW_DURATION } from "./constants";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { INTERVIEW_DURATION } from "./constants";
import { INTERVIEW_DURATION } from "@/constants";

Comment on lines 114 to 122
return (
await availabilitySlots.create([
{
userId: payload?.userId || 1,
start: start.toISOString(),
end: end.toISOString(),
},
])
)[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clean and prone to errors. Please refactor it.

Comment on lines 59 to 87
const canUseFullFlag =
after && before && dayjs.utc(before).diff(after, "days") <= 30;

const paginatedSlots = await availabilitySlots.find({
users: [userId],
after,
before,
pagination: {
page: pagination?.page || 1,
size: pagination?.size || 10,
full: pagination?.full || !!canUseFullFlag,
},
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const canUseFullFlag =
after && before && dayjs.utc(before).diff(after, "days") <= 30;
const paginatedSlots = await availabilitySlots.find({
users: [userId],
after,
before,
pagination: {
page: pagination?.page || 1,
size: pagination?.size || 10,
full: pagination?.full || !!canUseFullFlag,
},
});
const canUseFullFlag =
after && before && dayjs.utc(before).diff(after, "days") <= 30;
if (!canUseFullFlag) reutrn next(bad);
const paginatedSlots = await availabilitySlots.find({
users: [userId],
after,
before,
pagination,
});

Comment on lines 97 to 107
const result: IAvailabilitySlot.FindAvailabilitySlotsApiResponse = {
list: [
{
slots: paginatedSlots.list,
subslots: asSubSlots([
...paginatedLessons.list,
...userInterviews.list,
]),
},
],
total: paginatedSlots.total,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response sturcture is not a little bit misleading. The location of the paginated data is not right. I believe a better response sturcutre should look like this:

type Response = {
    slots: Paginated<IAvailabilitySlot.Self[]>,
    subslots: IAvailabilitySlot.SubSlot[],
}

The slots are pagianted that's why it has the Paginated type. Subslot isn't.

Comment on lines 74 to 95
if (slotsOnly && (isTutor(user.role) || isTutorManager(user.role))) {
const result = {
list: [{ slots: paginatedSlots.list, subslots: [] }],
total: paginatedSlots.total,
};
res.status(200).json(result);
return;
}

const slotIds = paginatedSlots.list.map((slot) => slot.id);

const paginatedLessons = await lessons.find({
users: [userId],
slots: slotIds,
after,
before,
});

const userInterviews = await interviews.find({
users: [userId],
slots: slotIds,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this logic into a seperate function. It will be more cleaner. Here is how the final code should look like.

function getSubslot(payload: {slotIds: number[], userId: number}): Promise<IAvailabilitySlot.SubSlot[]>;

const subslots = isTutor(user) || isTutorManager(user) ? [] : getSubSlot(slotIds, user.id);

This way we will only have one res.json() and the logic will relatively simple to grasb what this handler is doing from a first look.

}: {
currentUserId: number;
ids: number[];
tx?: Knex.Transaction;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the tx required to force the consumer to pass it.

Suggested change
tx?: Knex.Transaction;
tx: Knex.Transaction;

Comment on lines 22 to 32
await lessons.cancelBatch({
ids: associatedLessons.list.map((l) => l.id),
canceledBy: currentUserId,
tx,
});
await interviews.cancel({
ids: associatedInterviews.list.map((i) => i.ids.self),
canceledBy: currentUserId,
tx,
});
await availabilitySlots.markAsDeleted({ ids: ids, tx });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Empty line for readability.
  • Use Promise.all.
Suggested change
await lessons.cancelBatch({
ids: associatedLessons.list.map((l) => l.id),
canceledBy: currentUserId,
tx,
});
await interviews.cancel({
ids: associatedInterviews.list.map((i) => i.ids.self),
canceledBy: currentUserId,
tx,
});
await availabilitySlots.markAsDeleted({ ids: ids, tx });
await lessons.cancelBatch({
ids: associatedLessons.list.map((l) => l.id),
canceledBy: currentUserId,
tx,
});
await interviews.cancel({
ids: associatedInterviews.list.map((i) => i.ids.self),
canceledBy: currentUserId,
tx,
});
await availabilitySlots.markAsDeleted({ ids: ids, tx });

const user = req.user;
if (!isUser(user)) return next(forbidden());

const { userId, after, before, slotsOnly, pagination } = findPayload.parse(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the time being, it is not required to control the toggle of subslots on and off for outside. It might change in the future but not now. In this case we can remove this flag.

Suggested change
const { userId, after, before, slotsOnly, pagination } = findPayload.parse(
const { userId, after, before, pagination } = findPayload.parse(

@mmoehabb mmoehabb force-pushed the moehab/api-availability-slots branch 2 times, most recently from 6987738 to 425de44 Compare January 14, 2025 15:05
@neuodev
Copy link
Member

neuodev commented Jan 14, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 14, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@mmoehabb mmoehabb requested a review from neuodev January 14, 2025 15:09
@neuodev neuodev force-pushed the moehab/api-availability-slots branch from e607fc2 to f7d8990 Compare January 15, 2025 05:13
@neuodev neuodev enabled auto-merge January 15, 2025 05:13
@neuodev neuodev disabled auto-merge January 15, 2025 05:15
@neuodev
Copy link
Member

neuodev commented Jan 15, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 15, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev neuodev force-pushed the moehab/api-availability-slots branch from f7d8990 to 620c5fa Compare January 15, 2025 05:41
@neuodev neuodev force-pushed the moehab/api-availability-slots branch from 620c5fa to 8d1ebd7 Compare January 15, 2025 05:45
@neuodev
Copy link
Member

neuodev commented Jan 15, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev
Copy link
Member

neuodev commented Jan 15, 2025

🚀 Nova
🚀 Luna
🚀 Nebula
🚀 Dashboard

@neuodev neuodev merged commit 3a9f0b2 into master Jan 15, 2025
4 checks passed
@neuodev neuodev deleted the moehab/api-availability-slots branch January 15, 2025 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants