Skip to content

Commit

Permalink
Updates the change request form to populate with previously requested…
Browse files Browse the repository at this point in the history
… changes if they exist (#2585)

* Updates the change request form to populate with previously requested changes if they exist

* throw all but the 404, types on zendeskticketId

* Apply type narrowing

* Update function to throw error and just return Id

* Update app/lib/zendesk.server.ts

Co-authored-by: Leo Singer <[email protected]>

* Update app/routes/circulars._archive._index/route.tsx

Co-authored-by: Leo Singer <[email protected]>

* Add final check for undefined

* Prettier on zendesk server

---------

Co-authored-by: Leo Singer <[email protected]>
  • Loading branch information
dakota002 and lpsinger authored Sep 18, 2024
1 parent d97cde9 commit 54ea8ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
11 changes: 10 additions & 1 deletion app/lib/zendesk.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ export async function postZendeskRequest(request: ZendeskRequest) {
console.error(response)
throw new Error(`Request failed with status ${response.status}`)
}
return await response.json()

const responseJson = await response.json()
if (!responseJson.request.id) {
console.error(responseJson)
throw new Error(
'ZenDesk request succeeded, but did not return a request ID'
)
}

return responseJson.request.id
}

export async function closeZendeskTicket(ticketId: number) {
Expand Down
31 changes: 22 additions & 9 deletions app/routes/circulars._archive._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
circularRedirect,
createChangeRequest,
get,
getChangeRequest,
getChangeRequests,
moderatorGroup,
put,
Expand Down Expand Up @@ -111,15 +112,27 @@ export async function action({ request }: ActionFunctionArgs) {
if (!createdOnDate || !createdOn)
throw new Response(null, { status: 400 })

const {
request: { id: zendeskTicketId },
} = await postZendeskRequest({
requester: { name: user.name, email: user.email },
subject: `Change Request for Circular ${circularId}`,
comment: {
body: `${user.name} has requested an edit. Review at ${origin}/circulars`,
},
})
let zendeskTicketId: number | undefined

try {
zendeskTicketId = (
await getChangeRequest(parseFloat(circularId), user.sub)
).zendeskTicketId
} catch (err) {
if (!(err instanceof Response && err.status === 404)) throw err
}

if (!zendeskTicketId) {
zendeskTicketId = await postZendeskRequest({
requester: { name: user.name, email: user.email },
subject: `Change Request for Circular ${circularId}`,
comment: {
body: `${user.name} has requested an edit. Review at ${origin}/circulars`,
},
})
}

if (!zendeskTicketId) throw new Response(null, { status: 500 })

await createChangeRequest(
{
Expand Down
14 changes: 12 additions & 2 deletions app/routes/circulars.correction.$circularId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { useLoaderData } from '@remix-run/react'
import { getUser } from './_auth/user.server'
import { CircularEditForm } from './circulars.edit.$circularId/CircularEditForm'
import { formatAuthor } from './circulars/circulars.lib'
import { get, submitterGroup } from './circulars/circulars.server'
import {
get,
getChangeRequest,
submitterGroup,
} from './circulars/circulars.server'
import type { BreadcrumbHandle } from '~/root/Title'

export const handle: BreadcrumbHandle & SEOHandle = {
Expand All @@ -29,7 +33,13 @@ export async function loader({
const user = await getUser(request)
if (!user?.groups.includes(submitterGroup))
throw new Response(null, { status: 403 })
const circular = await get(parseFloat(circularId))
let existingRequest
try {
existingRequest = await getChangeRequest(parseFloat(circularId), user.sub)
} catch (err) {
if (!(err instanceof Response && err.status === 404)) throw err
}
const circular = existingRequest ?? (await get(parseFloat(circularId)))
const defaultDateTime = new Date(circular.createdOn ?? 0).toISOString()

return {
Expand Down

0 comments on commit 54ea8ce

Please sign in to comment.