summaryrefslogtreecommitdiff
path: root/src/routes/kid.ts
blob: a326d5bed3d053a9bbd72799ed498d5c9b7964e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import express from "express";
import { authDevice } from "../middleware/auth";
import { db } from "../db/db";
import { deviceConfig } from "../db/schema";
import { eq } from "drizzle-orm";
import { logger } from "../lib/pino";
import { z } from "zod";

/** Schema for validating device IDs from authenticated requests */
const DeviceIdSchema = z
  .number()
  .int()
  .positive("Device ID must be a positive integer");

const router: express.Router = express.Router();

router.get("/kid/getconfig", authDevice, async (req, res) => {
  const deviceId = req.user!.id;

  const parsed = DeviceIdSchema.safeParse(deviceId);
  if (!parsed.success) {
    logger.error(
      { deviceId, error: parsed.error },
      "Invalid device ID in getconfig request",
    );
    res.status(400).json({
      success: false,
      reason: parsed.error.issues[0]?.message || "Invalid device ID",
    });
    return;
  }

  try {
    let config;
    try {
      config = await db
        .select()
        .from(deviceConfig)
        .where(eq(deviceConfig.deviceId, deviceId))
        .limit(1);
    } catch (dbError) {
      logger.error(
        { error: dbError, deviceId },
        "Database error fetching device config",
      );
      throw dbError;
    }

    if (config.length === 0) {
      try {
        const newConfig = await db
          .insert(deviceConfig)
          .values({ deviceId })
          .returning();
        config = newConfig;
        logger.info({ deviceId }, "Created default device config");
      } catch (insertError) {
        logger.error(
          { error: insertError, deviceId },
          "Failed to create default device config",
        );
        throw insertError;
      }
    }

    const cfg = config[0];
    if (!cfg) {
      logger.error(
        { deviceId },
        "Config is unexpectedly undefined after creation",
      );
      res.status(500).json({
        success: false,
        reason: "Failed to get device configuration",
      });
      return;
    }

    logger.debug(
      {
        deviceId,
        config: {
          disableBuddy: cfg.disableBuddy,
          blockAdultSites: cfg.blockAdultSites,
        },
      },
      "Device config retrieved successfully",
    );

    res.json({
      success: true,
      config: {
        disableBuddy: cfg.disableBuddy,
        blockAdultSites: cfg.blockAdultSites,
        familyLinkAntiCircumvention: cfg.familyLinkAntiCircumvention,
      },
    });
  } catch (e) {
    logger.error({ error: e, deviceId }, "Failed to get device config");
    res.status(500).json({
      success: false,
      reason: "Failed to get device configuration",
    });
  }
});

export default router;