-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server"; | ||
import { DataSets, DataSource, type IDataSource } from "data"; | ||
import { type Character, IdSchema } from "model"; | ||
|
||
interface SuccessResult { | ||
success: true; | ||
character: Character; | ||
} | ||
|
||
interface ErrorResult { | ||
success: false; | ||
errorCode: "notFound" | "unexpectedError"; | ||
message?: string; | ||
} | ||
|
||
type Result = SuccessResult | ErrorResult; | ||
|
||
export async function retrieveCharacter(id: string): Promise<Result> { | ||
// Validate the parameter | ||
const parse = IdSchema.safeParse(id); | ||
if (!parse.success) { | ||
return { success: false, errorCode: "notFound" }; | ||
} | ||
|
||
// Validate the request | ||
const characterId: string = parse.data; | ||
const { getUser } = getKindeServerSession(); | ||
const user = await getUser(); | ||
const dataSource: IDataSource = new DataSource(); | ||
const characters = await dataSource.get(DataSets.Characters).find({ id: characterId, userId: user.id }); | ||
|
||
if (characters.length === 0) { | ||
return { success: false, errorCode: "notFound" }; | ||
} else if (characters.length > 1) { | ||
return { success: false, errorCode: "unexpectedError", message: "Multiple characters found with the same id" }; | ||
} else { | ||
return { success: true, character: characters[0] }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters