-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add ability to send email to all artists as an admin
- Loading branch information
Simon
committed
Feb 5, 2025
1 parent
eee807a
commit d75fa43
Showing
19 changed files
with
327 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Button from "components/common/Button"; | ||
import TextEditor from "components/common/TextEditor"; | ||
import WidthContainer from "components/common/WidthContainer"; | ||
import React from "react"; | ||
import { Controller, FormProvider, useForm } from "react-hook-form"; | ||
import api from "services/api"; | ||
import { useSnackbar } from "state/SnackbarContext"; | ||
import { useConfirm } from "utils/useConfirm"; | ||
|
||
interface FormData { | ||
content: string; | ||
} | ||
|
||
const AdminSendEmail = () => { | ||
const { ask } = useConfirm(); | ||
|
||
const snackbar = useSnackbar(); | ||
const methods = useForm<FormData>(); | ||
|
||
const updateSettings = React.useCallback( | ||
async (data: FormData) => { | ||
try { | ||
const ok = await ask( | ||
"Are you sure you want to send this to these users? With great power comes great responsibility." | ||
); | ||
if (!ok) { | ||
return; | ||
} | ||
const { result } = await api.post< | ||
{ content: string }, | ||
{ result: { sentTo: number } } | ||
>("admin/send-email", data); | ||
snackbar(`Sent email to ${result.sentTo} users!`, { type: "success" }); | ||
} catch (e) { | ||
console.error(e); | ||
snackbar("Oops something went wrong", { type: "warning" }); | ||
} | ||
}, | ||
[snackbar] | ||
); | ||
|
||
return ( | ||
<WidthContainer variant="medium"> | ||
<h3>Send Email</h3> | ||
<FormProvider {...methods}> | ||
<form onSubmit={methods.handleSubmit(updateSettings)}> | ||
<Controller | ||
name="content" | ||
render={({ field: { onChange, value } }) => { | ||
return ( | ||
<TextEditor | ||
onChange={(val: any) => { | ||
onChange(val); | ||
}} | ||
value={value} | ||
/> | ||
); | ||
}} | ||
/> | ||
<Button type="submit">Send email</Button> | ||
</form> | ||
</FormProvider> | ||
</WidthContainer> | ||
); | ||
}; | ||
|
||
export default AdminSendEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
doctype html | ||
html | ||
head | ||
block head | ||
meta(charset="utf-8") | ||
meta(name="viewport", content="width=device-width") | ||
meta(http-equiv="X-UA-Compatible", content="IE=edge") | ||
meta(name="x-apple-disable-message-reformatting") | ||
style | ||
include ../style.css | ||
body.sans-serif | ||
p | ||
div | ||
| !{content} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
= `Mirlo: Platform Announcement` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| | ||
| !{post.content} |
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20250205180543_add_receive_platform_emails_boolean/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "receivePlatformEmails" BOOLEAN NOT NULL DEFAULT true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { userAuthenticated, userHasPermission } from "../../../auth/passport"; | ||
import prisma from "@mirlo/prisma"; | ||
import { sendMailQueue } from "../../../queues/send-mail-queue"; | ||
|
||
export default function () { | ||
const operations = { | ||
POST: [userAuthenticated, userHasPermission("admin"), POST], | ||
}; | ||
|
||
async function POST(req: Request, res: Response, next: NextFunction) { | ||
const { content } = req.body; | ||
try { | ||
const users = await prisma.user.findMany({ | ||
where: { | ||
receivePlatformEmails: true, | ||
}, | ||
include: { | ||
artists: true, | ||
}, | ||
}); | ||
const usersWithArtists = users.filter((u) => u.artists.length > 0); | ||
|
||
await Promise.all( | ||
usersWithArtists.map(async (user) => { | ||
await sendMailQueue.add("send-mail", { | ||
template: "admin-announcement", | ||
message: { | ||
to: user.email, | ||
}, | ||
locals: { | ||
email: user.email, | ||
user, | ||
content, | ||
}, | ||
}); | ||
}) | ||
); | ||
return res.status(200).json({ | ||
result: { | ||
sentTo: usersWithArtists.length, | ||
}, | ||
}); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
return operations; | ||
} |
Oops, something went wrong.