transforming response and getting the right object back for the line

This commit is contained in:
talksik
2022-05-05 08:56:40 -05:00
parent d013212054
commit 7f5eba7732
7 changed files with 116 additions and 38 deletions
+66 -18
View File
@@ -16,6 +16,7 @@ import MasterLineData from "@nirvana/core/models/masterLineData.model";
import NirvanaResponse from "../../core/responses/nirvanaResponse"; import NirvanaResponse from "../../core/responses/nirvanaResponse";
import { ObjectId } from "mongodb"; import { ObjectId } from "mongodb";
import Relationship from "@nirvana/core/models/relationship.model"; import Relationship from "@nirvana/core/models/relationship.model";
import { User } from "@nirvana/core/models/user.model";
import { UserService } from "../services/user.service"; import { UserService } from "../services/user.service";
import { collections } from "../services/database.service"; import { collections } from "../services/database.service";
@@ -122,42 +123,89 @@ async function getUserLines(req: Request, res: Response) {
const userInfo = res.locals.userInfo as JwtClaims; const userInfo = res.locals.userInfo as JwtClaims;
// get all of user's lineMember entries // get all of user's lineMember entries
const lineMembers = await LineService.getLineMembersByUserId( const userLineMembers = await LineService.getLineMembersByUserId(
userInfo.userId userInfo.userId
); );
if (!lineMembers?.length) { if (!userLineMembers?.length) {
res.status(400).json(); res.status(400).json();
return; return;
} }
const lineIds = lineMembers?.map((lineMem) => lineMem.lineId) ?? []; const lineIds =
userLineMembers?.map((lineMember) => lineMember.lineId) ?? [];
// this will include the current user lineMember association to the line
const allLineMembers = await LineService.getLineMembersInLines(lineIds);
const allLinesUsers: ObjectId[] = [];
allLineMembers?.map((currentLineMember) => {
if (currentLineMember?._id) allLinesUsers.push(currentLineMember.userId);
}) ?? [];
// get all users relevant here
const allRelevantUsers = await UserService.getUsersByIds(allLinesUsers);
// get all lines from the list of relevant lines // get all lines from the list of relevant lines
const lines = (await LineService.getLinesByIds(lineIds)) ?? []; const lines = (await LineService.getLinesByIds(lineIds)) ?? [];
const masterLines = const masterLines: MasterLineData[] = [];
lines.map((currentLine) => {
const associatedLineMember = lineMembers.find((lineMember) =>
lineMember.lineId.equals(currentLine._id!)
);
// TODO: get all of the other members on the line // TODO: get the latest audio blocks for this line...maybe like today and yesterday or by block count
// TODO: get the latest audio blocks for this line...maybe like today and yesterday or by block count lines.map((currentLine) => {
let associatedLineMembersForLine: LineMember[] = [];
return new MasterLineData( // get all of the line members for this Line
currentLine._id!, // make sure that we don't add line member if it's the current user
currentLine.createdDate, allLineMembers?.map((lineMember) => {
currentLine.lastUpdatedDate, if (currentLine._id) {
associatedLineMember if (lineMember.lineId.equals(currentLine._id))
); associatedLineMembersForLine.push(lineMember);
}
}) ?? []; }) ?? [];
const resObj = new GetUserLinesResponse(lines); // get the user lineMember assoc out of the list of the lineMembers for this line
const userLineMember = associatedLineMembersForLine?.find(
(currentLineMemberForLine) =>
currentLineMemberForLine.userId.toString() === userInfo.userId
);
res.json(masterLines); console.log(associatedLineMembersForLine);
if (userLineMember) {
// take out the current user from the "other" line members list now
associatedLineMembersForLine = associatedLineMembersForLine.filter(
(currentLineMember) =>
currentLineMember.userId.toString() !== userInfo.userId
);
// get the user objects for all of the other members
const otherUsers: User[] = [];
associatedLineMembersForLine.forEach((currentLineMember) => {
const foundUserObject = allRelevantUsers?.find(
(currentUser) => currentUser._id === currentLineMember.userId
);
if (foundUserObject) otherUsers.push(foundUserObject);
});
masterLines.push(
new MasterLineData(
currentLine,
userLineMember,
associatedLineMembersForLine,
otherUsers
)
);
}
});
const resObj = new GetUserLinesResponse(masterLines);
res.json(new NirvanaResponse(resObj));
} catch (error) { } catch (error) {
res.status(500).json(error); res.status(500).json(error);
} }
+14
View File
@@ -46,6 +46,20 @@ export class LineService {
return null; return null;
} }
/** Get all of the members associated to the given list of lines */
static async getLineMembersInLines(lineIds: ObjectId[]) {
const query = { lineId: { $in: lineIds } };
const lineMembersRes = await collections.lineMembers?.find(query).toArray();
// exists
if (lineMembersRes?.length) {
return lineMembersRes as LineMember[];
}
return null;
}
static async createLine(line: Line, lineMembers: LineMember[]) { static async createLine(line: Line, lineMembers: LineMember[]) {
const session = client.startSession(); const session = client.startSession();
try { try {
+13
View File
@@ -19,6 +19,19 @@ export class UserService {
return null; return null;
} }
static async getUsersByIds(userIds: ObjectId[]) {
const query = { _id: { $in: userIds } };
const res = await collections.users?.find(query).toArray();
// exists
if (res?.length) {
return res as User[];
}
return undefined;
}
static async getUserByGoogleId(googleUserId: string) { static async getUserByGoogleId(googleUserId: string) {
const query = { googleId: googleUserId }; const query = { googleId: googleUserId };
+14 -13
View File
@@ -1,22 +1,23 @@
import AudioClip from "./audioClip.model"; import { Line, LineMember } from "./line.model";
import { LineMember } from "./line.model";
import { ObjectId } from "mongodb";
import AudioClip from "./audioClip.model";
import { ObjectId } from "mongodb";
import { User } from "./user.model";
// why do we have separate full objects being sent?
// speed...I'm developing full stack and I just want all of the data and don't want to change this model
// repeatedly and trace data back and forth
export default class MasterLineData { export default class MasterLineData {
constructor( constructor(
// attributes of conversation // full line object
public id: ObjectId, // id of the conversation public lineDetails: Line,
public createdDate: Date, // requesting user's association to the line
public lastUpdatedDate: Date, public currentUserMember: LineMember,
// compiled data for easy client read
public currentUserMember?: LineMember,
// all other members in the convo as well as their user object to see the member details
public otherMembers?: LineMember[], public otherMembers?: LineMember[],
public audioClips: AudioClip[] = [], public otherUserObjects?: User[] /**public audioClips: AudioClip[] = [], // public media: Media[] */
public name?: string
) {} ) {}
} }
@@ -1,5 +1,6 @@
import { Line } from "../models/line.model"; import { Line } from "../models/line.model";
import MasterLineData from "../models/masterLineData.model";
export default class GetUserLinesResponse { export default class GetUserLinesResponse {
constructor(public lines: Line[]) {} constructor(public masterLines: MasterLineData[]) {}
} }
+4 -3
View File
@@ -37,10 +37,11 @@ export function useUserSearch(searchQuery: string) {
}); });
} }
export function useUserConvos() { export function useUserLines() {
// todo: merge with sockets + audio clip data + master data + convomember data // todo: base/source of truth for getting all of the lines for the user
// merge with sockets + audio clip data + master data + convomember data
return useQuery("USER_CONVERSATIONS", ApiCalls.getUserConversations); return useQuery("USER_LINES", ApiCalls.getUserLines);
} }
// =========== MUTATIONS // =========== MUTATIONS
@@ -3,7 +3,7 @@ import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
import CreateLineRequest from "@nirvana/core/requests/createLine.request"; import CreateLineRequest from "@nirvana/core/requests/createLine.request";
import { Line } from "@nirvana/core/models/line.model"; import { Line } from "@nirvana/core/models/line.model";
import LoginResponse from "../../../core/responses/login.response"; import LoginResponse from "../../../core/responses/login.response";
import MasterConversation from "@nirvana/core/models/masterLineData.model"; import MasterLineData from "@nirvana/core/models/masterLineData.model";
import NirvanaResponse from "../../../core/responses/nirvanaResponse"; import NirvanaResponse from "../../../core/responses/nirvanaResponse";
import { User } from "@nirvana/core/models"; import { User } from "@nirvana/core/models";
import UserDetailsResponse from "../../../core/responses/userDetails.response"; import UserDetailsResponse from "../../../core/responses/userDetails.response";
@@ -82,7 +82,7 @@ async function userSearch(searchQuery: string): Promise<UserSearchResponse> {
); );
} }
async function getUserConversations(): Promise<MasterConversation[]> { async function getUserLines(): Promise<NirvanaResponse<MasterLineData[]>> {
return await NirvanaApi.fetch(`/lines`, "GET", true); return await NirvanaApi.fetch(`/lines`, "GET", true);
} }
@@ -101,7 +101,7 @@ export const ApiCalls = {
authCheck, authCheck,
getUserDetails, getUserDetails,
userSearch, userSearch,
getUserConversations, getUserLines,
getDmByUserId, getDmByUserId,
createLine, createLine,
}; };