Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 47x 58x 58x 58x 58x 58x 92x 92x 92x 58x 47x 26x 26x 26x 47x 29x 29x 29x 29x 29x 29x 29x 47x 41x 41x 41x 41x 15x 26x 41x 41x 41x | import { MongoDbError } from "shared/types/api/errors";
import { User } from "shared/types/models/user";
import {
Result,
makeErrorResult,
makeSuccessResult,
} from "shared/utils/result";
import { UserModel, UserSchemaDb } from "server/features/user/userSchema";
import { logger } from "server/utils/logger";
export const findGroupMembers = async (
groupCode: string,
): Promise<Result<User[], MongoDbError>> => {
try {
const response = await UserModel.find({ groupCode }).lean();
Iif (response.length === 0) {
logger.info(`MongoDB: group ${groupCode} not found`);
} else {
logger.debug(
`MongoDB: Found group ${groupCode} with ${response.length} members`,
);
}
const results = response.flatMap((user) => {
const result = UserSchemaDb.safeParse(user);
Iif (!result.success) {
logger.error(
new Error(
`Error validating findGroupMembers DB value: username: ${user.username}`,
{ cause: result.error },
),
);
return [];
}
return result.data;
});
return makeSuccessResult(results);
} catch (error) {
logger.error(
new Error(`MongoDB: Error finding group ${groupCode}`, { cause: error }),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
export const checkGroupExists = async (
groupCode: string,
): Promise<Result<boolean, MongoDbError>> => {
try {
const response = await UserModel.exists({
groupCode,
isGroupCreator: true,
});
return makeSuccessResult(!!response);
} catch (error) {
logger.error(
new Error(`MongoDB: Error checking if group ${groupCode} exists`, {
cause: error,
}),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
export const saveGroupCreator = async (
groupCode: string,
isGroupCreator: boolean,
username: string,
): Promise<Result<User, MongoDbError>> => {
try {
const response = await UserModel.findOneAndUpdate(
{ username },
{ groupCode, isGroupCreator },
{ returnDocument: "after" },
).lean();
Iif (!response) {
logger.info(`MongoDB: saveGroupCreator user ${username} not found`);
return makeErrorResult(MongoDbError.USER_NOT_FOUND);
}
logger.info(
`MongoDB: Saved group creator status ${isGroupCreator} for user ${username} with groupCode ${groupCode}`,
);
const result = UserSchemaDb.safeParse(response);
Iif (!result.success) {
logger.error(
new Error(`Error validating saveGroupCreator DB value`, {
cause: result.error,
}),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
return makeSuccessResult(result.data);
} catch (error) {
logger.error(
new Error(
`MongoDB: Error saving group creator status for user ${username}`,
{ cause: error },
),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
export const saveGroupCode = async (
groupCode: string,
username: string,
): Promise<Result<User, MongoDbError>> => {
try {
const response = await UserModel.findOneAndUpdate(
{ username },
{ groupCode },
{ returnDocument: "after" },
).lean();
Iif (!response) {
logger.info(`MongoDB: saveGroupCode user ${username} not found`);
return makeErrorResult(MongoDbError.USER_NOT_FOUND);
}
if (groupCode === "0") {
logger.info(`MongoDB: User ${username} left group`);
} else {
logger.info(`MongoDB: Group ${groupCode} stored for user ${username}`);
}
const result = UserSchemaDb.safeParse(response);
Iif (!result.success) {
logger.error(
new Error(`Error validating saveGroupCode DB value`, {
cause: result.error,
}),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
return makeSuccessResult(result.data);
} catch (error) {
logger.error(
new Error(
`MongoDB: Error storing group ${groupCode} for user ${username}`,
{ cause: error },
),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
|