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
2 changes: 1 addition & 1 deletion client/src/views/gatepass/StaffDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const StaffDashboard: React.FC = () => {
</p>
</div>
<div className="flex gap-2">
{checkAccess("gatepass:staff:bulk-create") && (
{checkAccess("gatepass:staff") && (
<Button onClick={() => setIsBulkCreateOpen(true)} variant="outline">
<Upload className="mr-2 h-4 w-4" /> Bulk Create
</Button>
Expand Down
4 changes: 1 addition & 3 deletions lib/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const allPermissions = {
"admin:role:delete": "Delete operations on roles",
"admin:tester": "Allows user to enter testing mode",
"gatepass:staff": "Create, view, invalidate a new gatepass for a visitor",
"gatepass:staff:bulk-create":
"Bulk create gatepasses for multiple visitors",
"gatepass:common": "Common operations for all gatepass users",
"gatepass:security": "Scan QR codes for check-in and check-out",
"gatepass:admin": "View all visits and gatepasses with admin privileges",
Expand All @@ -33,7 +31,7 @@ export const permissions: { [key: string]: keyof typeof allPermissions } = {
"/admin/role/delete": "admin:role:delete",
"/admin/permission/all": "admin:role:read",
"/gatepass/staff/create": "gatepass:staff",
"/gatepass/staff/bulk-create": "gatepass:staff:bulk-create",
"/gatepass/staff/bulk-create": "gatepass:staff",
"/gatepass/staff/list": "gatepass:staff",
"/gatepass/staff/invalidate": "gatepass:staff",
"/gatepass/security/dashboard": "gatepass:security",
Expand Down
28 changes: 24 additions & 4 deletions server/src/api/gatepass/staff/bulkCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import { asyncHandler } from "@/middleware/routeHandler.ts";
import { checkAccess } from "@/middleware/auth.ts";
import { gatepassSchemas } from "lib";
import db from "@/config/db/index.ts";
import { gatepasses, visitTypeEnum } from "@/config/db/schema/gatepass.ts";
// Import the enums to ensure type safety
import {
gatepasses,
visitTypeEnum,
gatepassStatusEnum,
} from "@/config/db/schema/gatepass.ts";
import { HttpCode, HttpError } from "@/config/errors.ts";
import assert from "assert";

const router = express.Router();

router.post(
"/",
checkAccess("gatepass:staff:bulk-create"),
checkAccess("gatepass:staff"),
asyncHandler(async (req, res) => {
assert(req.user, "User not authenticated");
const { visitors, category, subCategory, validFrom, validTo } =
Expand All @@ -24,16 +29,31 @@ router.post(
);
}

const now = new Date();
const fromDate = new Date(validFrom);
const todayStart = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate()
);

// --- Start of Fix ---
// Explicitly type the initialStatus variable to match the database enum.
const initialStatus: (typeof gatepassStatusEnum.enumValues)[number] =
fromDate > todayStart ? "scheduled" : "pending";
// --- End of Fix ---

const gatepassInserts = visitors.map((visitor) => ({
visitorName: visitor.visitorName,
visitorEmail: visitor.visitorEmail,
visitorPhone: visitor.visitorPhone,
category,
subCategory,
visitType: "official" as (typeof visitTypeEnum.enumValues)[number], // Correctly typed
validFrom: new Date(validFrom),
visitType: "official" as (typeof visitTypeEnum.enumValues)[number],
validFrom: fromDate,
validTo: new Date(validTo),
createdBy: req.user!.email,
status: initialStatus, // This is now correctly typed
}));

await db.insert(gatepasses).values(gatepassInserts);
Expand Down