|
| 1 | +/** |
| 2 | + * 募集ドメイン - ミューテーション(書き込み操作) |
| 3 | + * |
| 4 | + * 責務: |
| 5 | + * - シフト募集の作成 |
| 6 | + */ |
| 7 | +import { ConvexError, v } from "convex/values"; |
| 8 | +import { internal } from "../_generated/api"; |
| 9 | +import { mutation } from "../_generated/server"; |
| 10 | +import { RECRUITMENT_STATUS } from "../constants"; |
| 11 | +import { generateToken, requireShop, requireShopOwnerOrManager } from "../helpers"; |
| 12 | + |
| 13 | +// 日付形式バリデーション(YYYY-MM-DD形式) |
| 14 | +const isValidDateFormat = (date: string) => { |
| 15 | + const dateRegex = /^\d{4}-\d{2}-\d{2}$/; |
| 16 | + if (!dateRegex.test(date)) return false; |
| 17 | + const parsed = new Date(date); |
| 18 | + return !Number.isNaN(parsed.getTime()); |
| 19 | +}; |
| 20 | + |
| 21 | +// シフト募集作成 |
| 22 | +export const create = mutation({ |
| 23 | + args: { |
| 24 | + shopId: v.id("shops"), |
| 25 | + authId: v.string(), |
| 26 | + startDate: v.string(), |
| 27 | + endDate: v.string(), |
| 28 | + deadline: v.string(), |
| 29 | + }, |
| 30 | + handler: async (ctx, args) => { |
| 31 | + // 店舗存在チェック |
| 32 | + await requireShop(ctx, args.shopId); |
| 33 | + |
| 34 | + // 権限チェック(オーナーまたはマネージャーのみ) |
| 35 | + await requireShopOwnerOrManager(ctx, args.shopId, args.authId); |
| 36 | + |
| 37 | + // 日付形式バリデーション |
| 38 | + if (!isValidDateFormat(args.startDate)) { |
| 39 | + throw new ConvexError({ message: "開始日の形式が不正です", code: "INVALID_START_DATE" }); |
| 40 | + } |
| 41 | + if (!isValidDateFormat(args.endDate)) { |
| 42 | + throw new ConvexError({ message: "終了日の形式が不正です", code: "INVALID_END_DATE" }); |
| 43 | + } |
| 44 | + if (!isValidDateFormat(args.deadline)) { |
| 45 | + throw new ConvexError({ message: "締切日の形式が不正です", code: "INVALID_DEADLINE" }); |
| 46 | + } |
| 47 | + |
| 48 | + // ビジネスルールバリデーション(フロントエンドのzodスキーマと一致) |
| 49 | + if (args.startDate > args.endDate) { |
| 50 | + throw new ConvexError({ message: "終了日は開始日以降を指定してください", code: "END_BEFORE_START" }); |
| 51 | + } |
| 52 | + if (args.deadline >= args.startDate) { |
| 53 | + throw new ConvexError({ |
| 54 | + message: "締切日は開始日より前を指定してください", |
| 55 | + code: "DEADLINE_NOT_BEFORE_START", |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + // アクティブスタッフ数を取得(非削除かつ非退職) |
| 60 | + const activeStaffs = await ctx.db |
| 61 | + .query("staffs") |
| 62 | + .withIndex("by_shop", (q) => q.eq("shopId", args.shopId)) |
| 63 | + .filter((q) => q.and(q.neq(q.field("isDeleted"), true), q.neq(q.field("status"), "resigned"))) |
| 64 | + .collect(); |
| 65 | + |
| 66 | + const totalStaffCount = activeStaffs.length; |
| 67 | + |
| 68 | + // 各スタッフにマジックリンクトークンを生成・更新 |
| 69 | + const deadlineEnd = new Date(`${args.deadline}T23:59:59`).getTime(); |
| 70 | + const recipients: { email: string; magicLinkToken: string }[] = []; |
| 71 | + |
| 72 | + for (const staff of activeStaffs) { |
| 73 | + const token = generateToken(); |
| 74 | + await ctx.db.patch(staff._id, { |
| 75 | + magicLinkToken: token, |
| 76 | + magicLinkExpiresAt: deadlineEnd, |
| 77 | + }); |
| 78 | + recipients.push({ email: staff.email, magicLinkToken: token }); |
| 79 | + } |
| 80 | + |
| 81 | + // 募集作成 |
| 82 | + const recruitmentId = await ctx.db.insert("recruitments", { |
| 83 | + shopId: args.shopId, |
| 84 | + startDate: args.startDate, |
| 85 | + endDate: args.endDate, |
| 86 | + deadline: args.deadline, |
| 87 | + status: RECRUITMENT_STATUS[0], // "open" |
| 88 | + appliedCount: 0, |
| 89 | + totalStaffCount, |
| 90 | + createdBy: args.authId, |
| 91 | + createdAt: Date.now(), |
| 92 | + isDeleted: false, |
| 93 | + }); |
| 94 | + |
| 95 | + // 店舗情報を取得してメール送信をスケジュール |
| 96 | + const shop = await requireShop(ctx, args.shopId); |
| 97 | + if (recipients.length > 0) { |
| 98 | + await ctx.scheduler.runAfter(0, internal.email.actions.sendRecruitmentNotification, { |
| 99 | + shopName: shop.shopName, |
| 100 | + startDate: args.startDate, |
| 101 | + endDate: args.endDate, |
| 102 | + deadline: args.deadline, |
| 103 | + recipients, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + return { success: true, data: { recruitmentId, totalStaffCount } }; |
| 108 | + }, |
| 109 | +}); |
0 commit comments