* feat: Create many invites added. * Update src/pages/api/auth/invite.ts Co-authored-by: Jonathan <axis@axis.moe> * fix: Lowered limit. Co-authored-by: Jonathan <axis@axis.moe>
This commit is contained in:
parent
38cba9cb39
commit
05de3fed15
2 changed files with 46 additions and 14 deletions
|
@ -1,4 +1,4 @@
|
|||
import { ActionIcon, Avatar, Button, Card, Group, Modal, Select, SimpleGrid, Skeleton, Stack, Title } from '@mantine/core';
|
||||
import { ActionIcon, Avatar, Button, Card, Group, Modal, NumberInput, Select, SimpleGrid, Skeleton, Stack, Title } from '@mantine/core';
|
||||
import { useClipboard } from '@mantine/hooks';
|
||||
import { useForm } from '@mantine/form';
|
||||
import { useModals } from '@mantine/modals';
|
||||
|
@ -25,11 +25,13 @@ function CreateInviteModal({ open, setOpen, updateInvites }) {
|
|||
const form = useForm({
|
||||
initialValues: {
|
||||
expires: '30m',
|
||||
count: 1,
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmit = async values => {
|
||||
if (!expires.includes(values.expires)) return form.setFieldError('expires', 'Invalid expiration');
|
||||
if (values.count < 1 || values.count > 100) return form.setFieldError('count', 'Must be between 1 and 200');
|
||||
const expires_at = values.expires === 'never' ? null : new Date({
|
||||
'30m': Date.now() + 30 * 60 * 1000,
|
||||
'1h': Date.now() + 60 * 60 * 1000,
|
||||
|
@ -44,6 +46,7 @@ function CreateInviteModal({ open, setOpen, updateInvites }) {
|
|||
|
||||
const res = await useFetch('/api/auth/invite', 'POST', {
|
||||
expires_at,
|
||||
count: values.count,
|
||||
});
|
||||
|
||||
if (res.error) {
|
||||
|
@ -89,6 +92,17 @@ function CreateInviteModal({ open, setOpen, updateInvites }) {
|
|||
]}
|
||||
/>
|
||||
|
||||
<NumberInput
|
||||
label='Count'
|
||||
id='count'
|
||||
{...form.getInputProps('count')}
|
||||
precision={0}
|
||||
min={1}
|
||||
stepHoldDelay={200}
|
||||
stepHoldInterval={100}
|
||||
parser={(v:string) => Number(v.replace(/[^\d]/g, ''))}
|
||||
/>
|
||||
|
||||
<Group position='right' mt={22}>
|
||||
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
||||
<Button type='submit'>Create</Button>
|
||||
|
@ -196,4 +210,4 @@ export default function Users() {
|
|||
</SimpleGrid>
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,27 +12,45 @@ async function handler(req: NextApiReq, res: NextApiRes) {
|
|||
if (!user.administrator) return res.forbid('you arent an administrator');
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { expires_at } = req.body as { expires_at: string };
|
||||
const { expires_at, count } = req.body as { expires_at: string, count: number };
|
||||
|
||||
const expiry = expires_at ? new Date(expires_at) : null;
|
||||
if (expiry) {
|
||||
if (!expiry.getTime()) return res.bad('invalid date');
|
||||
if (expiry.getTime() < Date.now()) return res.bad('date is in the past');
|
||||
}
|
||||
const counts = count ? count : 1;
|
||||
|
||||
const code = randomChars(6);
|
||||
|
||||
const invite = await prisma.invite.create({
|
||||
data: {
|
||||
code,
|
||||
createdById: user.id,
|
||||
expires_at: expiry,
|
||||
},
|
||||
});
|
||||
if (counts > 1) {
|
||||
const data = [];
|
||||
for (let i = 0; i !== counts; ++i) {
|
||||
data.push({
|
||||
code: randomChars(8),
|
||||
createdById: user.id,
|
||||
expires_at: expiry,
|
||||
});
|
||||
}
|
||||
|
||||
Logger.get('invite').info(`${user.username} (${user.id}) created invite ${invite.code}`);
|
||||
await prisma.invite.createMany({data});
|
||||
|
||||
return res.json(invite);
|
||||
Logger.get('invite').info(`${user.username} (${user.id}) created ${data.length} invites with codes ${data.map(invite => invite.code).join(', ')}`);
|
||||
|
||||
return res.json(data);
|
||||
} else {
|
||||
const code = randomChars(6);
|
||||
|
||||
const invite = await prisma.invite.create({
|
||||
data: {
|
||||
code,
|
||||
createdById: user.id,
|
||||
expires_at: expiry,
|
||||
},
|
||||
});
|
||||
|
||||
Logger.get('invite').info(`${user.username} (${user.id}) created invite ${invite.code}`);
|
||||
|
||||
return res.json(invite);
|
||||
}
|
||||
} else if (req.method === 'GET') {
|
||||
const invites = await prisma.invite.findMany({
|
||||
orderBy: {
|
||||
|
|
Loading…
Add table
Reference in a new issue