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 | 40x 22x 22x 22x 22x 22x 22x 22x 22x | import { UserModel, UserSchemaDb } from "server/features/user/userSchema";
import { logger } from "server/utils/logger";
import { MongoDbError } from "shared/types/api/errors";
import { FavoriteProgramItemId, NewFavorite } from "shared/types/models/user";
import {
Result,
makeSuccessResult,
makeErrorResult,
} from "shared/utils/result";
export const saveFavorite = async (
newFavorite: NewFavorite,
): Promise<Result<readonly FavoriteProgramItemId[], MongoDbError>> => {
const { username, favoriteProgramItemIds } = newFavorite;
try {
const response = await UserModel.findOneAndUpdate(
{ username },
{
favoriteProgramItemIds,
},
{ returnDocument: "after" },
).lean();
Iif (!response) {
logger.error(new Error(`MongoDB: User ${username} not found`));
return makeErrorResult(MongoDbError.USER_NOT_FOUND);
}
logger.info(
`MongoDB: Favorite data stored for user ${newFavorite.username}`,
);
const result = UserSchemaDb.safeParse(response);
Iif (!result.success) {
logger.error(
new Error(
`Error validating saveFavorite DB value: ${JSON.stringify(result.error)}`,
),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
return makeSuccessResult(result.data.favoriteProgramItemIds);
} catch (error) {
logger.error(
new Error(
`MongoDB: Error storing favorite data for user ${newFavorite.username}`,
{ cause: error },
),
);
return makeErrorResult(MongoDbError.UNKNOWN_ERROR);
}
};
|