diff --git a/src/index.ts b/src/index.ts index 9bf0f94..c7d33eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ type Env = { securePodcastEndpoint: URL; secureSubjectEndpoint: URL; secureEpisodesOutgoingEndpoint: URL; + secureEpisodePublishEndpoint: URL; } const allowedOrigins: Array = [ @@ -461,6 +462,42 @@ app.post("/episode/:id", auth0Middleware, async (c) => { return c.json({ error: "Unauthorised" }, 403); }); +app.post("/episode/publish/:id", auth0Middleware, async (c) => { + const auth0Payload: Auth0JwtPayload = c.var.auth0('payload'); + const id = c.req.param('id') + c.header("Cache-Control", "max-age=600"); + c.header("Content-Type", "application/json"); + c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin"))); + c.header("Access-Control-Allow-Methods", "POST,GET,OPTIONS"); + + if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) { + const authorisation: string = c.req.header("Authorization")!; + const url = `${c.env.secureEpisodePublishEndpoint}/${id}`; + const data: any = await c.req.json(); + const body: string = JSON.stringify(data); + const resp = await fetch(url, { + headers: { + 'Accept': "*/*", + 'Authorization': authorisation, + "Content-type": "application/json", + "Cache-Control": "no-cache", + "User-Agent": "cult-podcasts-api", + "Host": new URL(c.env.secureEpisodePublishEndpoint).host + }, + method: "POST", + body: body + }); + if (resp.status == 202) { + console.log(`Successfully used secure-episode-endpoint.`); + return new Response(resp.body); + } else { + console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`); + return c.json({ error: "Error" }, 500); + } + } + return c.json({ error: "Unauthorised" }, 403); +}); + app.get("/episodes/outgoing", auth0Middleware, async (c) => { const auth0Payload: Auth0JwtPayload = c.var.auth0('payload'); c.header("Cache-Control", "max-age=600");