Skip to content

Commit

Permalink
Updates the change request functions to automatically close the corre…
Browse files Browse the repository at this point in the history
…sponding zendesk ticket (#2546)
  • Loading branch information
dakota002 authored Sep 5, 2024
1 parent 7bf8201 commit 6ab395c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
29 changes: 28 additions & 1 deletion app/lib/zendesk.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ export async function postZendeskRequest(request: ZendeskRequest) {

if (!response.ok) {
console.error(response)
throw new Error(`Reqeust failed with status ${response.status}`)
throw new Error(`Request failed with status ${response.status}`)
}
return await response.json()
}

export async function closeZendeskTicket(ticketId: number) {
const response = await fetch(
`https://nasa-gcn.zendesk.com/api/v2/tickets/${ticketId}.json`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...getBasicAuthHeaders(
`${getEnvOrDie('ZENDESK_TOKEN_EMAIL')}/token`,
getEnvOrDie('ZENDESK_TOKEN')
),
},
body: JSON.stringify({
ticket: {
status: 'solved',
},
}),
}
)

if (!response.ok) {
console.error(response)
throw new Error(`Request failed with status ${response.status}`)
}
}
19 changes: 12 additions & 7 deletions app/routes/circulars._archive._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,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`,
},
})

await createChangeRequest(
{
circularId: parseFloat(circularId),
...props,
submitter,
createdOn,
zendeskTicketId,
},
user
)
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`,
},
})
newCircular = null
break
case 'edit':
Expand Down
1 change: 1 addition & 0 deletions app/routes/circulars/circulars.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface CircularChangeRequest extends CircularMetadata {
format: CircularFormat
submitter: string
createdOn: number
zendeskTicketId: number
}

export interface CircularChangeRequestKeys {
Expand Down
49 changes: 27 additions & 22 deletions app/routes/circulars/circulars.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
} from './circulars.lib'
import { sendEmail } from '~/lib/email.server'
import { feature, origin } from '~/lib/env.server'
import { closeZendeskTicket } from '~/lib/zendesk.server'

// A type with certain keys required.
type Require<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
Expand Down Expand Up @@ -387,7 +388,7 @@ export async function createChangeRequest(
| 'editedOn'
| 'submitter'
| 'createdOn'
> & { submitter?: string; createdOn?: number },
> & { submitter?: string; createdOn?: number; zendeskTicketId: number },
user?: User
) {
validateCircular(item)
Expand Down Expand Up @@ -462,10 +463,9 @@ export async function deleteChangeRequest(
{ status: 403 }
)

const requestorEmail = (await getChangeRequest(circularId, requestorSub))
.requestorEmail
const changeRequest = await getChangeRequest(circularId, requestorSub)
const requestorEmail = changeRequest.requestorEmail
await deleteChangeRequestRaw(circularId, requestorSub)

await sendEmail({
to: [requestorEmail],
fromName: 'GCN Circulars',
Expand Down Expand Up @@ -527,27 +527,32 @@ export async function approveChangeRequest(
const circular = await get(circularId)
const autoincrementVersion = await getDynamoDBVersionAutoIncrement(circularId)

await autoincrementVersion.put({
...circular,
body: changeRequest.body,
subject: changeRequest.subject,
editedBy: `${formatAuthor(user)} on behalf of ${changeRequest.requestor}`,
editedOn: Date.now(),
format: changeRequest.format,
submitter: changeRequest.submitter,
createdOn: changeRequest.createdOn ?? circular.createdOn, // This is temporary while there are some requests without this property
})
const promises = [
autoincrementVersion.put({
...circular,
body: changeRequest.body,
subject: changeRequest.subject,
editedBy: `${formatAuthor(user)} on behalf of ${changeRequest.requestor}`,
editedOn: Date.now(),
format: changeRequest.format,
submitter: changeRequest.submitter,
createdOn: changeRequest.createdOn ?? circular.createdOn, // This is temporary while there are some requests without this property
}),
deleteChangeRequestRaw(circularId, requestorSub),
sendEmail({
to: [changeRequest.requestorEmail],
fromName: 'GCN Circulars',
subject: 'GCN Circulars Change Request: Approved',
body: dedent`Your change request has been approved for GCN Circular ${changeRequest.circularId}.
await deleteChangeRequestRaw(circularId, requestorSub)
View the Circular at ${origin}/circulars/${changeRequest.circularId}`,
}),
]

await sendEmail({
to: [changeRequest.requestorEmail],
fromName: 'GCN Circulars',
subject: 'GCN Circulars Change Request: Approved',
body: dedent`Your change request has been approved for GCN Circular ${changeRequest.circularId}.
if (changeRequest.zendeskTicketId)
promises.push(closeZendeskTicket(changeRequest.zendeskTicketId))

View the Circular at ${origin}/circulars/${changeRequest.circularId}`,
})
await Promise.all(promises)
}

/**
Expand Down

0 comments on commit 6ab395c

Please sign in to comment.