Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions email/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as gracely from "gracely"
import * as http from "cloudly-http"
import { Context } from "../Context"
import { router } from "../router"

interface Email {
recipient: string
subject: string
content: string
}
namespace Email {
export function is(value: Email | any): value is Email {
return (
typeof value == "object" &&
value &&
typeof value.recipient == "string" &&
typeof value.subject == "string" &&
typeof value.content == "string"
)
}
}

export async function create(request: http.Request, context: Context): Promise<http.Response.Like | any> {
let result: gracely.Error | Awaited<ReturnType<typeof context.email>>
const authorization = gracely.Error.is(context.authenticator)
? context.authenticator
: await context.authenticator.authenticate(request, "admin")
const body: Email | unknown = await request.body
if (gracely.Error.is(authorization))
result = authorization
else if (authorization != "admin")
result = gracely.client.unauthorized()
else if (!Email.is(body))
result = gracely.client.malformedContent("Email", "Email", "Body is missing either: subject, recipient or content.")
else {
result = await context.email(body.recipient, body.subject, body.content)
}
return result
}
router.add("POST", "/email", create)
1 change: 1 addition & 0 deletions email/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./create"
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./user"
import "./application"
import "./organization"
import "./me"
import "./email"

export { DurableApplication } from "./Context/Applications/Storage/"
export { DurableUser } from "./Context/Users/Storage"
Expand Down