Skip to content

Commit

Permalink
modif: models components and the server according to the new online s…
Browse files Browse the repository at this point in the history
…tatus implementation.
  • Loading branch information
mmoehabb committed Dec 17, 2024
1 parent e82742a commit 92b1d4b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 43 deletions.
11 changes: 0 additions & 11 deletions packages/models/src/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { first, merge, omit, orderBy } from "lodash";
import { IFilter, IRoom, Paginated } from "@litespace/types";
import { Knex } from "knex";
import dayjs from "@/lib/dayjs";
import zod from "zod";
import { Cache } from "@/cache";
import { users } from "@/users";
import { messages } from "@/messages";

Expand Down Expand Up @@ -261,18 +259,9 @@ export class Rooms {
}

async asPopulatedMember(row: IRoom.PopulatedMemberRow): Promise<IRoom.PopulatedMember> {
const redisUrl = zod.string({
message: "Missing PG_USER"
}).parse(process.env.REDIS_URL);

const cache = new Cache(redisUrl);
// TODO: make one perminant connection session for all models
await cache.connect();

return merge(omit(row, "createdAt", "updatedAt"), {
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
online: await cache.onlineStatus.isOnline(row.id)
});
}
}
Expand Down
11 changes: 0 additions & 11 deletions packages/models/src/tutors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { IUser, ITutor, IFilter, Paginated } from "@litespace/types";
import { Knex } from "knex";
import { users } from "@/users";
import dayjs from "@/lib/dayjs";
import zod from "zod";
import { Cache } from "@/cache";

type TutorMediaFieldsMap = Record<keyof ITutor.TutorMedia, string>;
type FullTutorFields = ITutor.FullTutorRow;
Expand Down Expand Up @@ -255,17 +253,8 @@ export class Tutors {
}

async asFullTutor(row: ITutor.FullTutorRow): Promise<ITutor.FullTutor> {
const redisUrl = zod.string({
message: "Missing PG_USER"
}).parse(process.env.REDIS_URL);

const cache = new Cache(redisUrl);
// TODO: make one perminant connection session for all models
await cache.connect();

return merge(omit(row), {
password: row.password !== null,
online: await cache.onlineStatus.isOnline(row.id),
});
}

Expand Down
13 changes: 1 addition & 12 deletions packages/models/src/users.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { column, countRows, knex, withFilter, withPagination } from "@/query";
import { column, countRows, knex, withPagination } from "@/query";
import { first, isEmpty } from "lodash";
import { IUser, Paginated } from "@litespace/types";
import { Knex } from "knex";
import dayjs from "@/lib/dayjs";
import zod from "zod";
import { Cache } from "@/cache";

export class Users {
public readonly table = "users" as const;
Expand Down Expand Up @@ -173,14 +171,6 @@ export class Users {
}

async from(row: IUser.Row): Promise<IUser.Self> {
const redisUrl = zod.string({
message: "Missing PG_USER"
}).parse(process.env.REDIS_URL);

const cache = new Cache(redisUrl);
// TODO: make one perminant connection session for all models
await cache.connect();

return {
id: row.id,
email: row.email,
Expand All @@ -190,7 +180,6 @@ export class Users {
birthYear: row.birth_year,
gender: row.gender,
role: row.role,
online: await cache.onlineStatus.isOnline(row.id),
verified: row.verified,
creditScore: row.credit_score,
phoneNumber: row.phone_number,
Expand Down
1 change: 0 additions & 1 deletion services/server/src/lib/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export async function asFindUserRoomsApiRecord({
id: otherMember.id,
name: otherMember.name,
image: otherMember.image,
online: otherMember.online,
role: otherMember.role,
lastSeen: otherMember.updatedAt,
},
Expand Down
18 changes: 10 additions & 8 deletions services/server/src/lib/tutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export async function constructTutorsCache(date: Dayjs): Promise<TutorsCache> {
}
);

const onlineUsers = await cache.onlineStatus.getAll();

// restruct tutors list to match ITutor.Cache[]
const cacheTutors: ITutor.Cache[] = onboardedTutors.map((tutor) => {
const filteredTopics = tutorsTopics
Expand All @@ -82,7 +84,7 @@ export async function constructTutorsCache(date: Dayjs): Promise<TutorsCache> {
bio: tutor.bio,
about: tutor.about,
gender: tutor.gender,
online: tutor.online,
online: onlineUsers[tutor.id] ? true : false,
notice: tutor.notice,
topics: filteredTopics,
avgRating:
Expand Down Expand Up @@ -191,14 +193,14 @@ async function findTutorCacheMeta(tutorId: number) {

export async function joinTutorCache(
tutor: ITutor.FullTutor,
cache: ITutor.Cache | null
cacheData: ITutor.Cache | null
): Promise<ITutor.Cache> {
const meta = cache
const meta = cacheData
? {
topics: cache.topics,
avgRating: cache.avgRating,
studentCount: cache.studentCount,
lessonCount: cache.lessonCount,
topics: cacheData.topics,
avgRating: cacheData.avgRating,
studentCount: cacheData.studentCount,
lessonCount: cacheData.lessonCount,
}
: await findTutorCacheMeta(tutor.id);

Expand All @@ -210,8 +212,8 @@ export async function joinTutorCache(
bio: tutor.bio,
about: tutor.about,
gender: tutor.gender,
online: tutor.online,
notice: tutor.notice,
online: await cache.onlineStatus.isOnline(tutor.id),
...meta,
};
}
Expand Down

0 comments on commit 92b1d4b

Please sign in to comment.