Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added .github/.DS_Store
Binary file not shown.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# プロダクト名
<!-- プロダクト名に変更してください -->
# ネボガード(NeboGuard)

![プロダクト名](https://kc3.me/cms/wp-content/uploads/2026/02/444e7120d5cdd74aa75f7a94bf8821a5-scaled.png)
![ネボガード(NeboGuard)](https://kc3.me/cms/wp-content/uploads/2026/02/444e7120d5cdd74aa75f7a94bf8821a5-scaled.png)
<!-- プロダクト名・イメージ画像を差し変えてください -->


Expand Down
6 changes: 6 additions & 0 deletions backend/migrations/0004_morning_routine_settings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table if not exists "morning_routine_settings" (
"user_id" text primary key,
"routine_json" text not null,
"created_at" text not null,
"updated_at" text not null
);
2 changes: 2 additions & 0 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAllowedOrigins, isAllowedOrigin } from "./lib/origins";
import { registerAuthRoutes } from "./routes/auth-routes";
import { registerBriefingRoutes } from "./routes/briefing-routes";
import { registerCalendarRoutes } from "./routes/calendar-routes";
import { registerMorningRoutineRoutes } from "./routes/morning-routine-routes";
import { registerRootRoutes } from "./routes/root-routes";
import { registerTaskRoutes } from "./routes/task-routes";
import { registerTransitRoutes } from "./routes/transit-routes";
Expand Down Expand Up @@ -53,6 +54,7 @@ export function createApp(): App {
registerRootRoutes(app);
registerAuthRoutes(app);
registerBriefingRoutes(app);
registerMorningRoutineRoutes(app);
registerCalendarRoutes(app);
registerTaskRoutes(app);
registerTransitRoutes(app);
Expand Down
162 changes: 162 additions & 0 deletions backend/src/features/morning-routine/morning-routine.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
export type MorningRoutineItem = {
id: string;
label: string;
minutes: number;
};

type RoutineRow = {
routine_json: string;
};

const DEFAULT_MORNING_ROUTINE: MorningRoutineItem[] = [
{ id: "prepare", label: "身支度", minutes: 20 },
{ id: "breakfast", label: "朝食", minutes: 15 },
];

function createRoutineItemId(): string {
if (
typeof crypto !== "undefined" &&
typeof crypto.randomUUID === "function"
) {
return crypto.randomUUID();
}
return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}

function clampMinutes(value: number): number {
if (!Number.isFinite(value)) {
return 0;
}
return Math.min(180, Math.max(0, Math.trunc(value)));
}

function cloneItems(items: MorningRoutineItem[]): MorningRoutineItem[] {
return items.map((item) => ({ ...item }));
}

function normalizeRoutineItems(value: unknown): MorningRoutineItem[] | null {
if (!Array.isArray(value)) {
return null;
}

if (value.length === 0 || value.length > 20) {
return null;
}

const normalized = value
.map((item) => {
if (!item || typeof item !== "object") {
return null;
}

const candidate = item as {
id?: unknown;
label?: unknown;
minutes?: unknown;
};

const label =
typeof candidate.label === "string" ? candidate.label.trim() : "";
if (label.length === 0 || label.length > 40) {
return null;
}

const minutesRaw =
typeof candidate.minutes === "number"
? candidate.minutes
: typeof candidate.minutes === "string"
? Number(candidate.minutes)
: Number.NaN;
const minutes = Number.isFinite(minutesRaw)
? clampMinutes(minutesRaw)
: 0;

const id =
typeof candidate.id === "string" && candidate.id.trim().length > 0
? candidate.id.trim()
: createRoutineItemId();

return { id, label, minutes } satisfies MorningRoutineItem;
})
.filter((item): item is MorningRoutineItem => item !== null);

if (normalized.length === 0) {
return null;
}

return normalized;
}

function parseRoutineJson(json: string): MorningRoutineItem[] | null {
try {
return normalizeRoutineItems(JSON.parse(json));
} catch {
return null;
}
}

export function validateMorningRoutineItems(
value: unknown,
): MorningRoutineItem[] | null {
return normalizeRoutineItems(value);
}

export async function getMorningRoutine(
db: D1Database,
userId: string,
): Promise<MorningRoutineItem[]> {
const row = await db
.prepare(
`
select routine_json
from morning_routine_settings
where user_id = ?
limit 1
`,
)
.bind(userId)
.first<RoutineRow>();

if (!row?.routine_json) {
return cloneItems(DEFAULT_MORNING_ROUTINE);
}

const parsed = parseRoutineJson(row.routine_json);
if (!parsed) {
return cloneItems(DEFAULT_MORNING_ROUTINE);
}

return parsed;
}

export async function saveMorningRoutine(
db: D1Database,
userId: string,
items: MorningRoutineItem[],
): Promise<MorningRoutineItem[]> {
const normalized = normalizeRoutineItems(items);
if (!normalized) {
throw new Error("Invalid morning routine items.");
}

const now = new Date().toISOString();
await db
.prepare(
`
insert into morning_routine_settings (
user_id,
routine_json,
created_at,
updated_at
)
values (?, ?, ?, ?)
on conflict(user_id) do update set
routine_json = excluded.routine_json,
updated_at = excluded.updated_at
`,
)
.bind(userId, JSON.stringify(normalized), now, now)
.run();

return normalized;
}
45 changes: 45 additions & 0 deletions backend/src/routes/morning-routine-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
getMorningRoutine,
saveMorningRoutine,
validateMorningRoutineItems,
} from "../features/morning-routine/morning-routine.service";
import { getAuthSession } from "../lib/session";
import type { App } from "../types/app";

export function registerMorningRoutineRoutes(app: App): void {
app.get("/briefing/routine", async (c) => {
const authSession = await getAuthSession(c);
if (!authSession) {
return c.json({ error: "Authentication required." }, 401);
}

const items = await getMorningRoutine(c.env.AUTH_DB, authSession.user.id);
return c.json({ items });
});

app.put("/briefing/routine", async (c) => {
const authSession = await getAuthSession(c);
if (!authSession) {
return c.json({ error: "Authentication required." }, 401);
}

const body = await c.req.json().catch(() => null);
const items = validateMorningRoutineItems(body?.items);
if (!items) {
return c.json(
{
error:
"Request body must include non-empty `items` with { id?, label, minutes }.",
},
400,
);
}

const saved = await saveMorningRoutine(
c.env.AUTH_DB,
authSession.user.id,
items,
);
return c.json({ items: saved });
});
}
2 changes: 2 additions & 0 deletions backend/src/routes/root-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export function registerRootRoutes(app: App): void {
endpoints: [
"ALL /api/auth/*",
"POST /briefing/morning",
"GET /briefing/routine",
"PUT /briefing/routine",
"GET /calendar/today",
"POST /transit/directions",
"POST /tasks/decompose",
Expand Down
Binary file added d76869c4364b9cc4.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading