-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working end to end but a lot of things to review / clean up.
- Loading branch information
Showing
12 changed files
with
317 additions
and
11 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
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
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
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
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
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
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
143 changes: 143 additions & 0 deletions
143
front/pages/api/w/[wId]/assistant/conversations/[cId]/content_fragment/index.ts
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,143 @@ | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getConversation, postNewContentFragment } from "@app/lib/api/assistant/conversation"; | ||
import { PostContentFragmentRequestBodySchema } from "@app/lib/api/assistant/types"; | ||
import { Authenticator, getSession } from "@app/lib/auth"; | ||
import { ReturnedAPIErrorType } from "@app/lib/error"; | ||
import { apiError, withLogging } from "@app/logger/withlogging"; | ||
import { ContentFragmentType } from "@app/types/assistant/conversation"; | ||
|
||
export const PostMessagesRequestBodySchema = t.type({ | ||
content: t.string, | ||
mentions: t.array( | ||
t.union([ | ||
t.type({ configurationId: t.string }), | ||
t.type({ | ||
provider: t.string, | ||
providerId: t.string, | ||
}), | ||
]) | ||
), | ||
context: t.type({ | ||
timezone: t.string, | ||
profilePictureUrl: t.union([t.string, t.null]), | ||
}), | ||
}); | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<{ contentFragment: ContentFragmentType; } | ReturnedAPIErrorType> | ||
): Promise<void> { | ||
const session = await getSession(req, res); | ||
const auth = await Authenticator.fromSession( | ||
session, | ||
req.query.wId as string | ||
); | ||
|
||
const owner = auth.workspace(); | ||
if (!owner) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "workspace_not_found", | ||
message: "The workspace you're trying to modify was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
const user = auth.user(); | ||
if (!user) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "workspace_user_not_found", | ||
message: "Could not find the user of the current session.", | ||
}, | ||
}); | ||
} | ||
|
||
if (!auth.isUser()) { | ||
return apiError(req, res, { | ||
status_code: 403, | ||
api_error: { | ||
type: "workspace_auth_error", | ||
message: | ||
"Only users of the current workspace can update chat sessions.", | ||
}, | ||
}); | ||
} | ||
if (!(typeof req.query.cId === "string")) { | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: "Invalid query parameters, `cId` (string) is required.", | ||
}, | ||
}); | ||
} | ||
|
||
const conversationId = req.query.cId; | ||
const conversation = await getConversation(auth, conversationId); | ||
if (!conversation) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "conversation_not_found", | ||
message: "Conversation not found.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "POST": | ||
const bodyValidation = PostContentFragmentRequestBodySchema.decode(req.body); | ||
|
||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
|
||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
|
||
const contentFragmentPayload = bodyValidation.right; | ||
|
||
const contentFragment = await postNewContentFragment(auth, { | ||
conversation, | ||
title: contentFragmentPayload.title, | ||
content: contentFragmentPayload.content, | ||
url: contentFragmentPayload.url, | ||
contentType: contentFragmentPayload.contentType, | ||
context: { | ||
timezone: contentFragmentPayload.context.timezone, | ||
username: user.username, | ||
fullName: user.fullName, | ||
email: user.email, | ||
profilePictureUrl: contentFragmentPayload.context.profilePictureUrl, | ||
|
||
}, | ||
}); | ||
|
||
|
||
res.status(200).json({ contentFragment }); | ||
return; | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, POST is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |
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
Oops, something went wrong.