mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-02-23 18:36:02 -05:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import {sendDiscordMessage} from 'common/discord/core'
|
|
import {jsonToMarkdown} from 'common/md'
|
|
import {tryCatch} from 'common/util/try-catch'
|
|
import {createSupabaseDirectClient} from 'shared/supabase/init'
|
|
import {insert} from 'shared/supabase/utils'
|
|
|
|
import {APIError, APIHandler} from './helpers/endpoint'
|
|
|
|
// Stores a contact message into the `contact` table
|
|
// Web sends TipTap JSON in `content`; we store it as string in `description`.
|
|
// If optional content metadata is provided, we include it; otherwise we fall back to user-centric defaults.
|
|
export const contact: APIHandler<'contact'> = async ({content, userId}, _auth) => {
|
|
const pg = createSupabaseDirectClient()
|
|
|
|
const {error} = await tryCatch(
|
|
insert(pg, 'contact', {
|
|
user_id: userId,
|
|
content: JSON.stringify(content),
|
|
}),
|
|
)
|
|
|
|
if (error) throw new APIError(500, 'Failed to submit contact message')
|
|
|
|
const continuation = async () => {
|
|
try {
|
|
let user = null
|
|
if (userId) {
|
|
user = await pg.oneOrNone(` select name from users where id = $1 `, [userId])
|
|
}
|
|
const md = jsonToMarkdown(content)
|
|
const tile = user ? `New message from ${user.name}` : 'New message'
|
|
const message: string = `**${tile}**\n${md}`
|
|
await sendDiscordMessage(message, 'contact')
|
|
} catch (e) {
|
|
console.error('Failed to send discord contact', e)
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
result: {},
|
|
continue: continuation,
|
|
}
|
|
}
|