This commit is contained in:
KM Koushik
2025-09-10 22:36:39 +10:00
parent 0817b0c7a5
commit 1ae6257c11
4 changed files with 30 additions and 23 deletions

View File

@@ -216,7 +216,7 @@ model ApiKey {
lastUsed DateTime? lastUsed DateTime?
teamId Int teamId Int
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
domain Domain? @relation(fields: [domainId], references: [id], onDelete: Cascade) domain Domain? @relation(fields: [domainId], references: [id], onDelete: SetNull, onUpdate: Cascade)
} }
enum EmailStatus { enum EmailStatus {

View File

@@ -33,8 +33,7 @@ import {
SelectItem, SelectItem,
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@unsend/ui/src/select"; } from "@usesend/ui/src/select";
const apiKeySchema = z.object({ const apiKeySchema = z.object({
name: z.string({ required_error: "Name is required" }).min(1, { name: z.string({ required_error: "Name is required" }).min(1, {
@@ -49,7 +48,7 @@ export default function AddApiKey() {
const createApiKeyMutation = api.apiKey.createToken.useMutation(); const createApiKeyMutation = api.apiKey.createToken.useMutation();
const [isCopied, setIsCopied] = useState(false); const [isCopied, setIsCopied] = useState(false);
const [showApiKey, setShowApiKey] = useState(false); const [showApiKey, setShowApiKey] = useState(false);
const domainsQuery = api.domain.domains.useQuery(); const domainsQuery = api.domain.domains.useQuery();
const utils = api.useUtils(); const utils = api.useUtils();
@@ -67,7 +66,8 @@ export default function AddApiKey() {
{ {
name: values.name, name: values.name,
permission: "FULL", permission: "FULL",
domainId: values.domainId === "all" ? undefined : Number(values.domainId), domainId:
values.domainId === "all" ? undefined : Number(values.domainId),
}, },
{ {
onSuccess: (data) => { onSuccess: (data) => {
@@ -75,7 +75,7 @@ export default function AddApiKey() {
setApiKey(data); setApiKey(data);
apiKeyForm.reset(); apiKeyForm.reset();
}, },
}, }
); );
} }
@@ -199,7 +199,10 @@ export default function AddApiKey() {
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>Domain access</FormLabel> <FormLabel>Domain access</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}> <Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl> <FormControl>
<SelectTrigger> <SelectTrigger>
<SelectValue placeholder="Select domain access" /> <SelectValue placeholder="Select domain access" />
@@ -207,11 +210,16 @@ export default function AddApiKey() {
</FormControl> </FormControl>
<SelectContent> <SelectContent>
<SelectItem value="all">All Domains</SelectItem> <SelectItem value="all">All Domains</SelectItem>
{domainsQuery.data?.map((domain: { id: number; name: string }) => ( {domainsQuery.data?.map(
<SelectItem key={domain.id} value={domain.id.toString()}> (domain: { id: number; name: string }) => (
{domain.name} <SelectItem
</SelectItem> key={domain.id}
))} value={domain.id.toString()}
>
{domain.name}
</SelectItem>
)
)}
</SelectContent> </SelectContent>
</Select> </Select>
<FormDescription> <FormDescription>

View File

@@ -60,17 +60,12 @@ function send(app: PublicAPIApp) {
const team = c.var.team; const team = c.var.team;
const emailId = c.req.param("emailId"); const emailId = c.req.param("emailId");
const whereClause: { id: string; teamId: number; domainId?: number } = {
id: emailId,
teamId: team.id,
};
if (team.apiKey.domainId !== null) {
whereClause.domainId = team.apiKey.domainId;
}
const email = await db.email.findUnique({ const email = await db.email.findUnique({
where: whereClause, where: {
id: emailId,
teamId: team.id,
domainId: team.apiKey.domainId ?? undefined,
},
select: { select: {
id: true, id: true,
teamId: true, teamId: true,

View File

@@ -48,7 +48,11 @@ function updateEmailScheduledAt(app: PublicAPIApp) {
const team = c.var.team; const team = c.var.team;
const emailId = c.req.param("emailId"); const emailId = c.req.param("emailId");
await checkIsValidEmailIdWithDomainRestriction(emailId, team.id, team.apiKey.domainId); await checkIsValidEmailIdWithDomainRestriction(
emailId,
team.id,
team.apiKey.domainId ?? undefined
);
await updateEmail(emailId, { await updateEmail(emailId, {
scheduledAt: c.req.valid("json").scheduledAt, scheduledAt: c.req.valid("json").scheduledAt,