perbaikan batas akses pengelola rumah ibadab sesuai dengan yang dipilih
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
import { eq, and, desc, count, sql } from "drizzle-orm";
|
||||
import { eq, and, desc, count, sql, inArray } from "drizzle-orm";
|
||||
import { getDb } from "./connection";
|
||||
import { distributions } from "@db/schema";
|
||||
import type { InsertDistribution } from "@db/schema";
|
||||
|
||||
export async function findAllDistributions(placeOfWorshipId?: number, recipientId?: number) {
|
||||
export async function findAllDistributions(placeOfWorshipIds?: number | number[], recipientId?: number) {
|
||||
const db = getDb();
|
||||
const conditions = [];
|
||||
if (placeOfWorshipId) conditions.push(eq(distributions.placeOfWorshipId, placeOfWorshipId));
|
||||
if (placeOfWorshipIds !== undefined) {
|
||||
if (Array.isArray(placeOfWorshipIds)) {
|
||||
if (placeOfWorshipIds.length > 0) {
|
||||
conditions.push(inArray(distributions.placeOfWorshipId, placeOfWorshipIds));
|
||||
} else {
|
||||
conditions.push(sql`1 = 0`);
|
||||
}
|
||||
} else {
|
||||
conditions.push(eq(distributions.placeOfWorshipId, placeOfWorshipIds));
|
||||
}
|
||||
}
|
||||
if (recipientId) conditions.push(eq(distributions.recipientId, recipientId));
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
return db.select().from(distributions).where(where).orderBy(desc(distributions.distributionDate));
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getDb } from "./connection";
|
||||
import { placesOfWorship } from "@db/schema";
|
||||
import type { InsertPlaceOfWorship } from "@db/schema";
|
||||
|
||||
export async function findAllPlaces(search?: string, type?: string, active?: string) {
|
||||
export async function findAllPlaces(search?: string, type?: string, active?: string, managerId?: number) {
|
||||
const db = getDb();
|
||||
const conditions = [];
|
||||
if (search) {
|
||||
@@ -15,6 +15,9 @@ export async function findAllPlaces(search?: string, type?: string, active?: str
|
||||
if (active && active !== "all") {
|
||||
conditions.push(eq(placesOfWorship.isActive, active as "yes" | "no"));
|
||||
}
|
||||
if (managerId !== undefined) {
|
||||
conditions.push(eq(placesOfWorship.managerId, managerId));
|
||||
}
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
return db.select().from(placesOfWorship).where(where).orderBy(desc(placesOfWorship.createdAt));
|
||||
}
|
||||
@@ -25,6 +28,21 @@ export async function findPlaceById(id: number) {
|
||||
return rows.at(0) ?? null;
|
||||
}
|
||||
|
||||
export async function findPlaceByManagerId(managerId: number) {
|
||||
const db = getDb();
|
||||
const rows = await db.select().from(placesOfWorship).where(eq(placesOfWorship.managerId, managerId)).limit(1);
|
||||
return rows.at(0) ?? null;
|
||||
}
|
||||
|
||||
export async function findPlaceIdsByManagerId(managerId: number) {
|
||||
const db = getDb();
|
||||
const rows = await db
|
||||
.select({ id: placesOfWorship.id })
|
||||
.from(placesOfWorship)
|
||||
.where(eq(placesOfWorship.managerId, managerId));
|
||||
return rows.map((r) => r.id);
|
||||
}
|
||||
|
||||
export async function createPlace(data: InsertPlaceOfWorship) {
|
||||
const db = getDb();
|
||||
const [{ id }] = await db.insert(placesOfWorship).values(data).$returningId();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { eq, like, and, count, desc } from "drizzle-orm";
|
||||
import { eq, like, and, count, desc, inArray, sql } from "drizzle-orm";
|
||||
import { getDb } from "./connection";
|
||||
import { recipients } from "@db/schema";
|
||||
import type { InsertRecipient } from "@db/schema";
|
||||
@@ -6,7 +6,7 @@ import type { InsertRecipient } from "@db/schema";
|
||||
export async function findAllRecipients(
|
||||
search?: string,
|
||||
status?: string,
|
||||
placeOfWorshipId?: number
|
||||
placeOfWorshipIds?: number | number[]
|
||||
) {
|
||||
const db = getDb();
|
||||
const conditions = [];
|
||||
@@ -16,8 +16,16 @@ export async function findAllRecipients(
|
||||
if (status && status !== "all") {
|
||||
conditions.push(eq(recipients.status, status as "pending" | "active" | "rejected" | "suspended"));
|
||||
}
|
||||
if (placeOfWorshipId) {
|
||||
conditions.push(eq(recipients.placeOfWorshipId, placeOfWorshipId));
|
||||
if (placeOfWorshipIds !== undefined) {
|
||||
if (Array.isArray(placeOfWorshipIds)) {
|
||||
if (placeOfWorshipIds.length > 0) {
|
||||
conditions.push(inArray(recipients.placeOfWorshipId, placeOfWorshipIds));
|
||||
} else {
|
||||
conditions.push(sql`1 = 0`);
|
||||
}
|
||||
} else {
|
||||
conditions.push(eq(recipients.placeOfWorshipId, placeOfWorshipIds));
|
||||
}
|
||||
}
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
return db.select().from(recipients).where(where).orderBy(desc(recipients.createdAt));
|
||||
@@ -29,11 +37,19 @@ export async function findRecipientById(id: number) {
|
||||
return rows.at(0) ?? null;
|
||||
}
|
||||
|
||||
export async function findPendingRecipients(placeOfWorshipId?: number) {
|
||||
export async function findPendingRecipients(placeOfWorshipIds?: number | number[]) {
|
||||
const db = getDb();
|
||||
const conditions = [eq(recipients.status, "pending")];
|
||||
if (placeOfWorshipId) {
|
||||
conditions.push(eq(recipients.placeOfWorshipId, placeOfWorshipId));
|
||||
if (placeOfWorshipIds !== undefined) {
|
||||
if (Array.isArray(placeOfWorshipIds)) {
|
||||
if (placeOfWorshipIds.length > 0) {
|
||||
conditions.push(inArray(recipients.placeOfWorshipId, placeOfWorshipIds));
|
||||
} else {
|
||||
conditions.push(sql`1 = 0`);
|
||||
}
|
||||
} else {
|
||||
conditions.push(eq(recipients.placeOfWorshipId, placeOfWorshipIds));
|
||||
}
|
||||
}
|
||||
return db.select().from(recipients)
|
||||
.where(and(...conditions))
|
||||
|
||||
Reference in New Issue
Block a user