Skip to content

Commit

Permalink
fix(server): update server db fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
neuodev committed Dec 1, 2024
1 parent 9a3ac76 commit 8c1b509
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
redis:
image: redis/redis-stack-server:7.2.0-v6
options: >-
--health-cmd 'redis-cli --raw incr ping'
--health-cmd "redis-cli --raw incr ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
Expand Down
246 changes: 83 additions & 163 deletions services/server/fixtures/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export async function flush() {
await interviews.builder(tx).del();
await calls.builder(tx).members.del();
await calls.builder(tx).calls.del();
await rules.builder(tx).del();
await lessons.builder(tx).members.del();
await lessons.builder(tx).lessons.del();
await interviews.builder(tx).del();
await rules.builder(tx).del();
await ratings.builder(tx).del();
await users.builder(tx).del();
});
Expand Down Expand Up @@ -99,70 +100,77 @@ export async function rule(payload?: Partial<IRule.CreatePayload>) {
});
}

const or = {
async tutorId(id?: number): Promise<number> {
if (!id) return await tutor().then((tutor) => tutor.id);
return id;
},
async studentId(id?: number): Promise<number> {
if (!id) return await student().then((student) => student.id);
return id;
},
async interviewerId(id?: number): Promise<number> {
if (!id) return await interviewer().then((interviewer) => interviewer.id);
return id;
},
async callId(id?: number): Promise<number> {
if (!id) return await calls.create().then((call) => call.id);
return id;
},
async ruleId(id?: number): Promise<number> {
if (!id) return await rule().then((rule) => rule.id);
return id;
},
start(start?: string): string {
if (!start) return faker.date.soon().toISOString();
return start;
},
} as const;

type LessonReturn = {
lesson: { self: ILesson.Self; members: ILesson.Member[] };
call: { self: ICall.Self; members: ICall.Member[] };
lesson: ILesson.Self;
members: ILesson.Member[];
};

export async function lesson(payload?: {
call?: Partial<ICall.CreatePayload>;
lesson?: Partial<ILesson.CreatePayload>;
}): Promise<LessonReturn> {
export async function lesson(
payload?: Partial<ILesson.CreatePayload> & {
timing?: "future" | "past";
canceled?: boolean;
}
): Promise<LessonReturn> {
return await knex.transaction(async (tx: Knex.Transaction) => {
const { call, members: callMembers } = await calls.create(
{
start: payload?.call?.start || faker.date.soon().toISOString(),
duration: payload?.call?.duration || duration(),
host: payload?.call?.host || 1,
members: payload?.call?.members || [],
rule: payload?.call?.rule || 1,
},
tx
);
const tutor = await or.tutorId(payload?.tutor);
const student = await or.studentId(payload?.student);
const data = await lessons.create({
call: await or.callId(payload?.call),
start:
payload?.timing === "future"
? faker.date.future().toISOString()
: payload?.timing === "past"
? faker.date.past().toISOString()
: payload?.start || faker.date.soon().toISOString(),
duration: payload?.duration || sample([15, 30]),
price: payload?.price || faker.number.int(500),
rule: await or.ruleId(payload?.rule),
student,
tutor,
tx,
});

const { lesson, members: lessonMembers } = await lessons.create(
{
call: call.id,
host: payload?.lesson?.host || 1,
members: payload?.lesson?.members || [],
price: payload?.lesson?.price || faker.number.int(500),
},
tx
);
if (payload?.canceled)
await lessons.cancel({ canceledBy: tutor, id: data.lesson.id, tx });

return {
lesson: { self: lesson, members: lessonMembers },
call: { self: call, members: callMembers },
};
return data;
});
}

export async function interview(payload: Partial<IInterview.CreatePayload>) {
return await knex.transaction(async (tx: Knex.Transaction) => {
const interviewerId: number =
payload.interviewer || (await interviewer().then((user) => user.id));

const intervieweeId: number =
payload.interviewee || (await tutor().then((user) => user.id));

const callId: number =
payload.call ||
(await call(
{
host: interviewerId,
members: [intervieweeId],
},
tx
).then(({ call }) => call.id));

return await interviews.create(
{
interviewer: interviewerId,
interviewee: intervieweeId,
call: callId,
},
tx
);
return await interviews.create({
interviewer: await or.interviewerId(payload.interviewer),
interviewee: await or.tutorId(payload.interviewee),
call: await or.callId(payload.call),
rule: await or.ruleId(payload.rule),
start: or.start(payload.start),
});
}

Expand All @@ -175,21 +183,6 @@ export async function topic(payload?: Partial<ITopic.CreatePayload>) {
});
}

export async function cancelLesson({
lesson,
call,
user,
}: {
lesson: number;
call: number;
user: number;
}) {
await knex.transaction(async (tx: Knex.Transaction) => {
await calls.cancel(call, user, tx);
await lessons.cancel(lesson, user, tx);
});
}

function tutor() {
return user({ role: IUser.Role.Tutor });
}
Expand All @@ -210,65 +203,6 @@ async function makeTutors(count: number) {
return await Promise.all(range(0, count).map(() => tutor()));
}

async function call(
payload: Partial<ICall.CreatePayload>,
tx: Knex.Transaction
) {
const host: number =
payload.host || (await tutor().then((tutor) => tutor.id));
const members: number[] = payload.members || [
await student().then((student) => student.id),
];
const callRule: number =
payload.rule ||
(await rule({
userId: host,
}).then((rule) => rule.id));

return await calls.create(
{
host,
members,
duration: payload.duration || duration(),
rule: callRule,
start: payload.start || faker.date.anytime().toISOString(),
},
tx
);
}

async function makeLesson({
future,
canceled,
}: {
future?: boolean;
canceled?: boolean;
}) {
const tutor = await user({ role: IUser.Role.Tutor });
const student = await user({ role: IUser.Role.Student });
const rule_ = await rule({ userId: tutor.id });
const now = dayjs.utc();
const start = future ? now.add(1, "week") : now.subtract(1, "week");
const { lesson: lesson_, call } = await lesson({
call: {
host: tutor.id,
members: [student.id],
rule: rule_.id,
start: start.toISOString(),
},
lesson: { host: tutor.id, members: [student.id] },
});

if (canceled) {
await knex.transaction(async (tx: Knex.Transaction) => {
await calls.cancel(call.self.id, tutor.id, tx);
await lessons.cancel(lesson_.self.id, tutor.id, tx);
});
}

return { lesson: lesson_, tutor, student, rule };
}

export type MakeLessonsReturn = Array<{
future: LessonReturn[];
past: LessonReturn[];
Expand Down Expand Up @@ -304,7 +238,7 @@ async function makeLessons({
past: number[];
};
}): Promise<MakeLessonsReturn> {
const lessons: MakeLessonsReturn = [];
const result: MakeLessonsReturn = [];

for (const [key, student] of entries(students)) {
const index = Number(key);
Expand All @@ -315,14 +249,12 @@ async function makeLessons({

const create = async (start: string) => {
return await lesson({
call: {
host: tutor,
members: [student],
rule: rule,
start,
duration,
},
lesson: { host: tutor, members: [student], price },
tutor,
student,
rule,
start,
duration,
price,
});
};

Expand Down Expand Up @@ -351,46 +283,38 @@ async function makeLessons({
// cancel future lessons
const canceledFutureLessons = await Promise.all(
range(0, canceledFutureLessonCount).map(async (i) => {
const lesson = futureLessons[i];
const info = futureLessons[i];
if (!lesson) throw new Error("invalid future lesson index");
await cancelLesson({
lesson: lesson.lesson.self.id,
call: lesson.call.self.id,
user: tutor,
});
return lesson;
await lessons.cancel({ canceledBy: tutor, id: info.lesson.id });
return info;
})
);

// cancel past lessons
const canceledPastLessons = await Promise.all(
range(0, canceledPastLessonCount).map(async (i) => {
const lesson = pastLessons[i];
if (!lesson) throw new Error("invalid past lesson index");
await cancelLesson({
lesson: lesson.lesson.self.id,
call: lesson.call.self.id,
user: tutor,
});
return lesson;
const info = pastLessons[i];
if (!info) throw new Error("invalid past lesson index");
await lessons.cancel({ canceledBy: tutor, id: info.lesson.id });
return info;
})
);

const canceledFutureLessonIds = canceledFutureLessons.map(
(future) => future.lesson.self.id
(future) => future.lesson.id
);
const canceledPastLessonIds = canceledPastLessons.map(
(past) => past.lesson.self.id
(past) => past.lesson.id
);
// filter out canceled future lessons
const uncanceledFutureLessons = futureLessons.filter(
(future) => !canceledFutureLessonIds.includes(future.lesson.self.id)
(future) => !canceledFutureLessonIds.includes(future.lesson.id)
);
// filter out canceled past lessons
const uncanceledPastLessons = pastLessons.filter(
(past) => !canceledPastLessonIds.includes(past.lesson.self.id)
(past) => !canceledPastLessonIds.includes(past.lesson.id)
);
lessons.push({
result.push({
future: futureLessons,
past: pastLessons,
canceled: {
Expand All @@ -404,7 +328,7 @@ async function makeLessons({
});
}

return lessons;
return result;
}

export async function makeRating(payload?: Partial<IRating.CreatePayload>) {
Expand Down Expand Up @@ -518,14 +442,10 @@ export default {
room: makeRoom,
message: makeMessage,
make: {
lesson: makeLesson,
lessons: makeLessons,
interviews: makeInterviews,
tutors: makeTutors,
rating: makeRating,
ratings: makeRatings,
},
cancel: {
lesson: cancelLesson,
},
};
4 changes: 2 additions & 2 deletions services/server/fixtures/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ async function execute(command: string): Promise<string> {
}

export async function down() {
await execute("pnpm --filter @litespace/models migrate:test down");
await execute("pnpm --filter @litespace/models migrate:test:local down");
}

export async function up() {
await execute("pnpm --filter @litespace/models migrate:test up");
await execute("pnpm --filter @litespace/models migrate:test:local up");
}

export async function flush() {
Expand Down
9 changes: 8 additions & 1 deletion services/server/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ module.exports = {
testEnvironment: "node",
transformIgnorePatterns: ["<rootDir>/node_modules/"],
transform: {
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(ts|tsx)?$": [
"ts-jest",
{
diagnostics: {
exclude: ["**"],
},
},
],
"^.+\\.(js|jsx)$": "babel-jest",
},
moduleNameMapper: {
Expand Down
2 changes: 1 addition & 1 deletion services/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start:test:server": "pnpm build && env-cmd -f .env.test ts-node dist/index.js",
"dev": "env-cmd nodemon src/index.ts",
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"test": "env-cmd -f .env.test jest -i --detectOpenHandles",
"test": "env-cmd -f .env.test jest --runInBand --detectOpenHandles --forceExit",
"pm2:start": "pm2 start ecosystem.config.js",
"peer": "peerjs --port 7070 --key peer --path /ls",
"docker": "docker compose up -d",
Expand Down

0 comments on commit 8c1b509

Please sign in to comment.