feat: add list controls and DNS check history
This commit is contained in:
parent
11b16ac3cf
commit
24351eb897
14 changed files with 511 additions and 28 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { createDb, domain } from "@minmon/db";
|
||||
import { createDb, domain, domainCheckHistory } from "@minmon/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { resolve4 } from "node:dns/promises";
|
||||
import { Effect } from "effect";
|
||||
|
|
@ -46,22 +46,41 @@ export async function persistDomainCheck(
|
|||
database: ReturnType<typeof createDb>,
|
||||
input: {
|
||||
domainId: string;
|
||||
expectedServerIp: string;
|
||||
result: DomainCheckResult;
|
||||
},
|
||||
) {
|
||||
const [updatedDomain] = await database
|
||||
.update(domain)
|
||||
.set({
|
||||
resolutionStatus: input.result.status,
|
||||
lastResolvedIps: input.result.resolvedIps,
|
||||
lastResolvedIp: input.result.resolvedIps[0] ?? null,
|
||||
lastCheckedAt: input.result.checkedAt,
|
||||
lastCheckMessage: input.result.message,
|
||||
})
|
||||
.where(eq(domain.id, input.domainId))
|
||||
.returning();
|
||||
return database.transaction(async (transaction) => {
|
||||
const [historyEntry] = await transaction
|
||||
.insert(domainCheckHistory)
|
||||
.values({
|
||||
id: crypto.randomUUID(),
|
||||
domainId: input.domainId,
|
||||
expectedServerIp: input.expectedServerIp,
|
||||
status: input.result.status,
|
||||
resolvedIps: input.result.resolvedIps,
|
||||
message: input.result.message,
|
||||
checkedAt: input.result.checkedAt,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return updatedDomain ?? null;
|
||||
const [updatedDomain] = await transaction
|
||||
.update(domain)
|
||||
.set({
|
||||
resolutionStatus: input.result.status,
|
||||
lastResolvedIps: input.result.resolvedIps,
|
||||
lastResolvedIp: input.result.resolvedIps[0] ?? null,
|
||||
lastCheckedAt: input.result.checkedAt,
|
||||
lastCheckMessage: input.result.message,
|
||||
})
|
||||
.where(eq(domain.id, input.domainId))
|
||||
.returning();
|
||||
|
||||
return {
|
||||
domain: updatedDomain ?? null,
|
||||
historyEntry: historyEntry ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function runAndPersistDomainCheck(
|
||||
|
|
@ -79,13 +98,15 @@ export async function runAndPersistDomainCheck(
|
|||
}),
|
||||
);
|
||||
|
||||
const updatedDomain = await persistDomainCheck(database, {
|
||||
const persisted = await persistDomainCheck(database, {
|
||||
domainId: input.domainId,
|
||||
expectedServerIp: input.expectedServerIp,
|
||||
result,
|
||||
});
|
||||
|
||||
return {
|
||||
result,
|
||||
domain: updatedDomain,
|
||||
domain: persisted.domain,
|
||||
historyEntry: persisted.historyEntry,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { db } from "@minmon/db";
|
||||
import { domain, service } from "@minmon/db";
|
||||
import { domain, domainCheckHistory, service } from "@minmon/db";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
import { runAndPersistDomainCheck } from "../lib/domain-check";
|
||||
|
|
@ -37,6 +37,16 @@ export const domainsRouter = router({
|
|||
return requireDomain(input.id);
|
||||
}),
|
||||
|
||||
history: permissionedProcedure("domains.read").input(byIdSchema).query(async ({ input }) => {
|
||||
await requireDomain(input.id);
|
||||
|
||||
return db
|
||||
.select()
|
||||
.from(domainCheckHistory)
|
||||
.where(eq(domainCheckHistory.domainId, input.id))
|
||||
.orderBy(desc(domainCheckHistory.checkedAt), desc(domainCheckHistory.id));
|
||||
}),
|
||||
|
||||
create: permissionedProcedure("domains.create").input(domainCreateSchema).mutation(async ({ input }) => {
|
||||
const { service: parentService, server: parentServer } = await requireServiceWithServer(input.serviceId);
|
||||
|
||||
|
|
@ -96,6 +106,7 @@ export const domainsRouter = router({
|
|||
return {
|
||||
domain: result.domain ?? existing,
|
||||
check: result.result,
|
||||
historyEntry: result.historyEntry,
|
||||
};
|
||||
}),
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue