Skip to content

Commit

Permalink
fix: rebased on master and errors have been fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoehabb committed Jan 14, 2025
1 parent 93d9749 commit 6987738
Show file tree
Hide file tree
Showing 28 changed files with 306 additions and 190 deletions.
3 changes: 3 additions & 0 deletions packages/atlas/src/atlas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { User } from "@/user";
import { Auth } from "@/auth";
import { AvailabilitySlot } from "@/availabilitySlot";
import { Backend } from "@litespace/types";
import { Plan } from "@/plan";
import { Coupon } from "@/coupon";
Expand All @@ -22,6 +23,7 @@ import { Session } from "@/session";
export class Atlas {
public readonly user: User;
public readonly auth: Auth;
public readonly availabilitySlot: AvailabilitySlot;
public readonly plan: Plan;
public readonly coupon: Coupon;
public readonly invite: Invite;
Expand All @@ -42,6 +44,7 @@ export class Atlas {
constructor(backend: Backend, token: AuthToken | null) {
this.user = new User(backend, token);
this.auth = new Auth(backend, token);
this.availabilitySlot = new AvailabilitySlot(backend, token);
this.plan = new Plan(backend, token);
this.coupon = new Coupon(backend, token);
this.invite = new Invite(backend, token);
Expand Down
3 changes: 3 additions & 0 deletions packages/headless/src/lessons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,21 @@ export function useCreateLesson({
async ({
tutorId,
ruleId,
slotId,
start,
duration,
}: {
tutorId: number;
ruleId: number;
slotId: number;
start: string;
duration: ILesson.Duration;
}) => {
return await atlas.lesson.create({
tutorId,
duration,
ruleId,
slotId,
start,
});
},
Expand Down
24 changes: 19 additions & 5 deletions packages/models/fixtures/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
users,
ratings,
tutors,
availabilitySlots,
} from "@/index";
import {
IInterview,
Expand All @@ -20,18 +21,17 @@ import {
IRating,
IMessage,
ISession,
IAvailabilitySlot,
} from "@litespace/types";
import { faker } from "@faker-js/faker/locale/ar";
import { entries, first, range, sample } from "lodash";
import { Knex } from "knex";
import dayjs from "@/lib/dayjs";
import { Time } from "@litespace/sol/time";
import { availabilitySlots } from "@/availabilitySlot";
import { randomUUID } from "crypto";

export async function flush() {
await knex.transaction(async (tx) => {
await availabilitySlots.builder(tx).del();
await topics.builder(tx).userTopics.del();
await topics.builder(tx).topics.del();
await messages.builder(tx).del();
Expand All @@ -44,6 +44,7 @@ export async function flush() {
await rules.builder(tx).del();
await ratings.builder(tx).del();
await tutors.builder(tx).del();
await availabilitySlots.builder(tx).del();
await users.builder(tx).del();
});
}
Expand Down Expand Up @@ -178,13 +179,18 @@ export async function lesson(
duration: payload?.duration || sample([15, 30]),
price: payload?.price || faker.number.int(500),
rule: await or.ruleId(payload?.rule),
slot: await or.slotId(payload?.slot),
student,
tutor,
tx,
});

if (payload?.canceled)
await lessons.cancel({ canceledBy: tutor, id: data.lesson.id, tx });
await lessons.cancel({
canceledBy: tutor,
ids: [data.lesson.id],
tx,
});

return data;
});
Expand All @@ -196,6 +202,7 @@ export async function interview(payload: Partial<IInterview.CreatePayload>) {
interviewee: await or.tutorId(payload.interviewee),
session: await or.sessionId("interview"),
rule: await or.ruleId(payload.rule),
slot: await or.slotId(payload.slot),
start: or.start(payload.start),
});
}
Expand Down Expand Up @@ -312,7 +319,10 @@ async function makeLessons({
range(0, canceledFutureLessonCount).map(async (i) => {
const info = futureLessons[i];
if (!lesson) throw new Error("invalid future lesson index");
await lessons.cancel({ canceledBy: tutor, id: info.lesson.id });
await lessons.cancel({
canceledBy: tutor,
ids: [info.lesson.id],
});
return info;
})
);
Expand All @@ -322,7 +332,10 @@ async function makeLessons({
range(0, canceledPastLessonCount).map(async (i) => {
const info = pastLessons[i];
if (!info) throw new Error("invalid past lesson index");
await lessons.cancel({ canceledBy: tutor, id: info.lesson.id });
await lessons.cancel({
canceledBy: tutor,
ids: [info.lesson.id],
});
return info;
})
);
Expand Down Expand Up @@ -464,6 +477,7 @@ export default {
flush,
topic,
rule,
slot,
room: makeRoom,
rating: makeRating,
message: makeMessage,
Expand Down
3 changes: 0 additions & 3 deletions packages/models/migrations/1716146586880_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ exports.up = (pgm) => {
duration: { type: "SMALLINT", notNull: true },
price: { type: "INT", notNull: true },
rule_id: { type: "SERIAL", references: "rules(id)", notNull: true },
<<<<<<< HEAD
=======
slot_id: {
type: "SERIAL",
references: "availability_slots(id)",
notNull: true,
},
>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.)
session_id: { type: "VARCHAR(50)", notNull: true, primaryKey: true },
canceled_by: { type: "INT", references: "users(id)", default: null },
canceled_at: { type: "TIMESTAMP", default: null },
Expand Down
8 changes: 4 additions & 4 deletions packages/models/scripts/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
invoices,
hashPassword,
topics,
availabilitySlots,
} from "@litespace/models";
import { IInterview, ILesson, IUser, IWithdrawMethod } from "@litespace/types";
import dayjs from "dayjs";
Expand Down Expand Up @@ -285,8 +286,6 @@ async function main(): Promise<void> {
monthday: sample(range(1, 31)),
});

<<<<<<< HEAD
=======
// seeding slots
await availabilitySlots.create(
addedTutors.map((tutor) => ({
Expand All @@ -296,7 +295,6 @@ async function main(): Promise<void> {
}))
);

>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.)
const times = range(0, 24).map((hour) =>
[hour.toString().padStart(2, "0"), "00"].join(":")
);
Expand Down Expand Up @@ -379,12 +377,13 @@ async function main(): Promise<void> {
start,
duration,
rule: 1,
slot: 1,
tx,
});

if (sample([0, 1]))
await lessons.cancel({
id: lesson.id,
ids: [lesson.id],
canceledBy: sample([tutorId, student.id]),
tx,
});
Expand Down Expand Up @@ -432,6 +431,7 @@ async function main(): Promise<void> {
interviewer: tutorManager.id,
start: randomStart(),
rule: rule.id,
slot: slot.id,
tx,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { IAvailabilitySlot } from "@litespace/types";
import { IAvailabilitySlot, IFilter, Paginated } from "@litespace/types";
import dayjs from "@/lib/dayjs";
import { Knex } from "knex";
import { first, isEmpty } from "lodash";
<<<<<<< HEAD:packages/models/src/availabilitySlot.ts
import { knex, column, WithOptionalTx } from "@/query";

type SearchFilter = {
/**
=======
import {
knex,
column,
Expand All @@ -22,7 +16,6 @@ type SearchFilter = {
*/
slots?: number[];
/**
>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.):packages/models/src/availabilitySlots.ts
* User ids to be included in the search query.
*/
users?: number[];
Expand All @@ -36,11 +29,8 @@ type SearchFilter = {
* All slots before (or the same as) this date will be included.
*/
before?: string;
<<<<<<< HEAD:packages/models/src/availabilitySlot.ts
=======
deleted?: boolean;
pagination?: IFilter.SkippablePagination;
>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.):packages/models/src/availabilitySlots.ts
};

export class AvailabilitySlots {
Expand Down Expand Up @@ -106,18 +96,17 @@ export class AvailabilitySlots {
async find({
tx,
users,
slots,
after,
before,
}: WithOptionalTx<SearchFilter>): Promise<IAvailabilitySlot.Self[]> {
pagination,
}: WithOptionalTx<SearchFilter>): Promise<Paginated<IAvailabilitySlot.Self>> {
const baseBuilder = this.applySearchFilter(this.builder(tx), {
users,
slots,
after,
before,
});
<<<<<<< HEAD:packages/models/src/availabilitySlot.ts
const rows = await baseBuilder.clone().select();
return rows.map((row) => this.from(row));
=======
const total = await countRows(baseBuilder.clone());
const rows = await withSkippablePagination(baseBuilder.clone(), pagination);
return {
Expand All @@ -132,7 +121,6 @@ export class AvailabilitySlots {
): Promise<IAvailabilitySlot.Self | null> {
const { list } = await this.find({ slots: [id], tx });
return first(list) || null;
>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.):packages/models/src/availabilitySlots.ts
}

async isOwner({
Expand All @@ -151,9 +139,14 @@ export class AvailabilitySlots {
return count === slots.length;
}

/**
* NOTE: this function filters out the marked-as-deleted rows
*/
async allExist(slots: number[], tx?: Knex.Transaction): Promise<boolean> {
if (isEmpty(slots)) return true;
const builder = this.builder(tx).whereIn(this.column("id"), slots);
const builder = this.builder(tx)
.where(this.column("deleted"), false)
.whereIn(this.column("id"), slots);
const count = await countRows(builder.clone());
return Number(count) === slots.length;
}
Expand Down
1 change: 1 addition & 0 deletions packages/models/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { coupons } from "@/coupons";
export { interviews } from "@/interviews";
export { availabilitySlots } from "@/availabilitySlots";
export { invites } from "@/invites";
export { lessons } from "@/lessons";
export { messages } from "@/messages";
Expand Down
12 changes: 8 additions & 4 deletions packages/models/src/interviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Interviews {
interviewer_feedback: this.column("interviewer_feedback"),
interviewee_feedback: this.column("interviewee_feedback"),
rule_id: this.column("rule_id"),
slot_id: this.column("slot_id"),
session_id: this.column("session_id"),
note: this.column("note"),
level: this.column("level"),
Expand All @@ -44,6 +45,7 @@ export class Interviews {
interviewee_id: payload.interviewee,
session_id: payload.session,
rule_id: payload.rule,
slot_id: payload.slot,
created_at: now,
updated_at: now,
})
Expand Down Expand Up @@ -138,6 +140,10 @@ export class Interviews {
return await this.findManyBy("rule_id", id);
}

async findBySlotId(id: number): Promise<IInterview.Self[]> {
return await this.findManyBy("slot_id", id);
}

async find({
statuses,
signers,
Expand All @@ -147,6 +153,7 @@ export class Interviews {
page,
size,
rules = [],
slots = [],
cancelled,
tx,
}: {
Expand All @@ -157,16 +164,12 @@ export class Interviews {
signed?: boolean;
signers?: number[];
rules?: number[];
<<<<<<< HEAD
cancelled?: boolean;
=======
/**
* slots ids to be included in the query result
*/
slots?: number[];
cancelled?: boolean;
pagination?: IFilter.SkippablePagination;
>>>>>>> 1861159d (add: find and set handlers for availability slots with unit tests.)
} & IFilter.Pagination): Promise<Paginated<IInterview.Self>> {
const baseBuilder = this.builder(tx);

Expand Down Expand Up @@ -217,6 +220,7 @@ export class Interviews {
interviewer: row.interviewer_id,
interviewee: row.interviewee_id,
rule: row.rule_id,
slot: row.slot_id,
session: row.session_id,
},
feedback: {
Expand Down
Loading

0 comments on commit 6987738

Please sign in to comment.