Skip to content

Commit

Permalink
Adds zendesk request function, post request when correction is reques…
Browse files Browse the repository at this point in the history
…ted (nasa-gcn#2188)

Resolves nasa-gcn#2114.
  • Loading branch information
dakota002 authored Apr 10, 2024
1 parent ffd5f0c commit 29f2a7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
48 changes: 48 additions & 0 deletions app/lib/zendesk.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { getEnvOrDie } from './env.server'
import { getBasicAuthHeaders } from './headers.server'

interface ZendeskRequest {
requester: Requester
subject: string
comment: RequestComment
}

interface Requester {
email: string
name: string
}

interface RequestComment {
body: string
}

export async function postZendeskRequest(request: ZendeskRequest) {
const response = await fetch(
'https://nasa-gcn.zendesk.com/api/v2/requests.json',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getBasicAuthHeaders(
`${getEnvOrDie('ZENDESK_TOKEN_EMAIL')}/token`,
getEnvOrDie('ZENDESK_TOKEN')
),
},
body: JSON.stringify({
request,
}),
}
)

if (!response.ok) {
console.error(response)
throw new Error(`Reqeust failed with status ${response.status}`)
}
}
12 changes: 11 additions & 1 deletion app/routes/circulars._archive._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ import CircularsIndex from './CircularsIndex'
import { DateSelector } from './DateSelectorMenu'
import { SortSelector } from './SortSelectorButton'
import Hint from '~/components/Hint'
import { feature } from '~/lib/env.server'
import { feature, origin } from '~/lib/env.server'
import { getFormDataString } from '~/lib/utils'
import { postZendeskRequest } from '~/lib/zendesk.server'
import { useModStatus } from '~/root'

import searchImg from 'nasawds/src/img/usa-icons-bg/search--white.svg'
Expand Down Expand Up @@ -100,10 +101,19 @@ export async function action({ request }: ActionFunctionArgs) {
case 'correction':
if (!circularId)
throw new Response('circularId is required', { status: 400 })
if (!user?.name || !user.email) throw new Response(null, { status: 403 })

await createChangeRequest(
{ circularId: parseFloat(circularId), ...props },
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
35 changes: 8 additions & 27 deletions app/routes/contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ import { validate } from 'email-validator'
import { useState } from 'react'

import { ReCAPTCHA, verifyRecaptcha } from '~/components/ReCAPTCHA'
import { getEnvOrDie, origin } from '~/lib/env.server'
import {
getBasicAuthHeaders,
getCanonicalUrlHeaders,
pickHeaders,
} from '~/lib/headers.server'
import { origin } from '~/lib/env.server'
import { getCanonicalUrlHeaders, pickHeaders } from '~/lib/headers.server'
import { getFormDataString } from '~/lib/utils'
import { postZendeskRequest } from '~/lib/zendesk.server'
import { useEmail, useName, useRecaptchaSiteKey } from '~/root'
import type { BreadcrumbHandle } from '~/root/Title'

Expand Down Expand Up @@ -58,27 +55,11 @@ export async function action({ request }: ActionFunctionArgs) {
}
)

const response = await fetch(
'https://nasa-gcn.zendesk.com/api/v2/requests.json',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getBasicAuthHeaders(
`${getEnvOrDie('ZENDESK_TOKEN_EMAIL')}/token`,
getEnvOrDie('ZENDESK_TOKEN')
),
},
body: JSON.stringify({
request: { requester: { name, email }, subject, comment: { body } },
}),
}
)
if (!response.ok) {
console.error(response)
throw new Error(`Reqeust failed with status ${response.status}`)
}

await postZendeskRequest({
requester: { name, email },
subject,
comment: { body },
})
return { email, subject }
}

Expand Down

0 comments on commit 29f2a7c

Please sign in to comment.