ALTER TABLE "company_settings" ADD COLUMN "check_in_radius_meters" integer DEFAULT 100 NOT NULL; --> statement-breakpoint CREATE TABLE "attendances" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "employee_id" uuid NOT NULL, "branch_id" uuid NOT NULL, "date" bigint NOT NULL, "check_in_at" bigint NOT NULL, "check_in_method" text NOT NULL, "check_in_latitude" double precision NOT NULL, "check_in_longitude" double precision NOT NULL, "check_in_photo_url" text, "check_in_distance_meters" integer, "check_out_at" bigint, "check_out_method" text, "check_out_latitude" double precision, "check_out_longitude" double precision, "check_out_photo_url" text, "check_out_distance_meters" integer, "status" text DEFAULT 'draft' NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "created_by" uuid NOT NULL, "updated_by" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE "visits" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "employee_id" uuid NOT NULL, "customer_id" uuid NOT NULL, "attendance_id" uuid, "plan_id" uuid, "plan_destination_id" uuid, "date" bigint NOT NULL, "check_in_at" bigint NOT NULL, "check_in_method" text NOT NULL, "check_in_latitude" double precision NOT NULL, "check_in_longitude" double precision NOT NULL, "check_in_photo_url" text, "check_in_distance_meters" integer, "check_out_at" bigint, "check_out_method" text, "check_out_latitude" double precision, "check_out_longitude" double precision, "check_out_photo_url" text, "check_out_distance_meters" integer, "status" text DEFAULT 'draft' NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "created_by" uuid NOT NULL, "updated_by" uuid NOT NULL ); --> statement-breakpoint ALTER TABLE "attendances" ADD CONSTRAINT "attendances_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE restrict ON UPDATE no action; --> statement-breakpoint ALTER TABLE "attendances" ADD CONSTRAINT "attendances_branch_id_branches_id_fk" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE restrict ON UPDATE no action; --> statement-breakpoint ALTER TABLE "attendances" ADD CONSTRAINT "attendances_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action; --> statement-breakpoint ALTER TABLE "attendances" ADD CONSTRAINT "attendances_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE restrict ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE restrict ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_attendance_id_attendances_id_fk" FOREIGN KEY ("attendance_id") REFERENCES "public"."attendances"("id") ON DELETE set null ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE set null ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_plan_destination_id_plan_destinations_id_fk" FOREIGN KEY ("plan_destination_id") REFERENCES "public"."plan_destinations"("id") ON DELETE set null ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action; --> statement-breakpoint ALTER TABLE "visits" ADD CONSTRAINT "visits_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action; --> statement-breakpoint CREATE UNIQUE INDEX "attendances_employee_date_live_unique" ON "attendances" USING btree ("employee_id","date") WHERE "attendances"."status" <> 'archived'; --> statement-breakpoint CREATE UNIQUE INDEX "attendances_employee_open_unique" ON "attendances" USING btree ("employee_id") WHERE "attendances"."check_out_at" IS NULL AND "attendances"."status" <> 'archived'; --> statement-breakpoint CREATE INDEX "attendances_employee_id_idx" ON "attendances" USING btree ("employee_id"); --> statement-breakpoint CREATE INDEX "attendances_branch_id_idx" ON "attendances" USING btree ("branch_id"); --> statement-breakpoint CREATE UNIQUE INDEX "visits_employee_open_unique" ON "visits" USING btree ("employee_id") WHERE "visits"."check_out_at" IS NULL AND "visits"."status" <> 'archived'; --> statement-breakpoint CREATE INDEX "visits_employee_id_idx" ON "visits" USING btree ("employee_id"); --> statement-breakpoint CREATE INDEX "visits_customer_id_idx" ON "visits" USING btree ("customer_id"); --> statement-breakpoint CREATE INDEX "visits_attendance_id_idx" ON "visits" USING btree ("attendance_id"); --> statement-breakpoint INSERT INTO "privilege_keys" ("code", "label", "sort_order") VALUES ('FIELD.ATTENDANCE', 'Branch attendance', 18), ('FIELD.VISIT', 'Customer visits', 19);