Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions apps/web-app/server/api/tilda/fila.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,35 @@ interface IIKOOrderItem {
comment: string
}

async function createAccessToken(): Promise<{ token: string, correlationId: string }> {
async function createAccessToken(): Promise<string> {
const { iiko } = useRuntimeConfig()

const apiUrl = 'https://api-ru.iiko.services/api/1/access_token'
const body = {
apiLogin: iiko.filaApiToken,
}

return fetch(apiUrl, {
const { token } = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}).then((res) => res.json())
}).then((res) => res.json()) as { token: string, correlationId: string }

return token
}

async function sendToIntegration(data: TildaFilaBody) {
const logger = useLogger('api:tilda:fila')
const { iiko } = useRuntimeConfig()

try {
const { token } = await createAccessToken()
const token = await createAccessToken()
if (!token) {
logger.error('Failed to create access token')
throw createError({ statusCode: 500, message: 'Failed to create access token' })
}

const apiUrl = 'https://api-ru.iiko.services/api/1/deliveries/create'

Expand Down Expand Up @@ -156,7 +162,7 @@ async function sendToIntegration(data: TildaFilaBody) {
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(body),
})
}).then((res) => res.json())
} catch (error) {
logger.error(error)
}
Expand Down