Skip to content

Commit

Permalink
Added rename podcast endpoint (#49)
Browse files Browse the repository at this point in the history
* Added rename podcast endpoint

* Added handling for response

* Updated route
  • Loading branch information
cultpodcasts authored Oct 9, 2024
1 parent 63681fd commit 9083ff7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { publish } from './publish';
import { getOutgoing } from './getOutgoing';
import { getPodcastByName } from './getPodcastByName';
import { updatePodcast } from './updatePodcast';
import { renamePodcast } from "./renamePodcast";
import { indexPodcastByName } from './indexPodcastByName';
import { getSubjectByName } from './getSubjectByName';
import { updateSubject } from './updateSubject';
Expand Down Expand Up @@ -47,5 +48,6 @@ app.post("/discovery-curation", Auth0Middleware, submitDiscovery);
app.post("/searchindex/run", Auth0Middleware, runSearchIndexer);
app.post("/publish/homepage", Auth0Middleware, publishHomepage);
app.post("/terms", Auth0Middleware, publishTerm);
app.post("/podcast/name/:name", Auth0Middleware, renamePodcast)

export default app;
38 changes: 38 additions & 0 deletions src/renamePodcast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AddResponseHeaders } from "./AddResponseHeaders";
import { Auth0ActionContext } from "./Auth0ActionContext";
import { Auth0JwtPayload } from "./Auth0JwtPayload";
import { buildFetchHeaders } from "./buildFetchHeaders";

export async function renamePodcast(c: Auth0ActionContext): Promise<Response> {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const podcastName = c.req.param('name');
const newName = c.req.param('newName');
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS"] });
if (auth0Payload?.permissions && auth0Payload.permissions.includes('admin')) {
const url = `${c.env.securePodcastEndpoint}/name/${podcastName}`;
const data: any = await c.req.json();
const body: string = JSON.stringify(data);
const resp = await fetch(url, {
headers: buildFetchHeaders(c.req, c.env.securePodcastEndpoint),
method: "POST",
body: body
});
if (resp.status == 200) {
console.log(`Successfully used secure-podcast-endpoint to rename podcast.`);
return new Response(resp.body);
} else if (resp.status == 400) {
console.log(`Unable to find podcast to rename podcast.`);
return new Response(resp.body, { status: resp.status });
} else if (resp.status == 404) {
console.log(`Unable to find podcast to rename podcast. Podccast not found.`);
return new Response(resp.body, { status: resp.status });
} else if (resp.status == 409) {
console.log(`Unable to find podcast to rename podcast. Podcast exists with new-name.`);
return new Response(resp.body, { status: resp.status });
} else {
console.log(`Failed to use secure-podcast-endpoint to rename podcast. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
}
2 changes: 1 addition & 1 deletion src/updatePodcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function updatePodcast(c: Auth0ActionContext): Promise<Response> {
const data: any = await c.req.json();
const body: string = JSON.stringify(data);
const resp = await fetch(url, {
headers: buildFetchHeaders(c.req, c.env.secureEpisodeEndpoint),
headers: buildFetchHeaders(c.req, c.env.securePodcastEndpoint),
method: "POST",
body: body
});
Expand Down

0 comments on commit 9083ff7

Please sign in to comment.