Skip to content
Draft
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
908 changes: 908 additions & 0 deletions COMPREHENSIVE_REVIEW_SUMMARY.md

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/app/api/inventory/adjust/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export async function POST(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId
stores: {
some: {
id: storeId
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/inventory/bulk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ export async function POST(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId
stores: {
some: {
id: storeId
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/inventory/history/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export async function GET(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId
stores: {
some: {
id: storeId
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/inventory/low-stock/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export async function GET(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId
stores: {
some: {
id: storeId
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/app/api/store-staff/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export async function GET(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId,
stores: {
some: {
id: storeId,
},
},
},
},
Expand Down Expand Up @@ -117,8 +119,10 @@ export async function POST(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: validatedData.storeId,
stores: {
some: {
id: validatedData.storeId,
},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/stores/[id]/role-requests/[requestId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<boolea
},
memberships: {
where: {
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
},
select: { id: true },
},
Expand All @@ -345,7 +345,7 @@ async function checkStoreAdminAccess(userId: string, storeId: string): Promise<b
},
memberships: {
where: {
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
role: { in: ['OWNER', 'ADMIN'] },
},
select: { id: true },
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/stores/[id]/role-requests/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<boolea
},
memberships: {
where: {
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
},
select: { id: true },
},
Expand Down Expand Up @@ -394,7 +394,7 @@ async function checkStoreAdminAccess(userId: string, storeId: string): Promise<b
},
memberships: {
where: {
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
role: { in: ['OWNER', 'ADMIN'] },
},
select: { id: true },
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/stores/[id]/staff/[staffId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<{ hasA
where: {
userId,
role: 'OWNER',
organization: { store: { id: storeId } }
organization: { stores: { some: { id: storeId } } }
},
});

Expand All @@ -46,7 +46,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<{ hasA
}

const membership = await prisma.membership.findFirst({
where: { userId, organization: { store: { id: storeId } } },
where: { userId, organization: { stores: { some: { id: storeId } } } },
});

if (membership) {
Expand Down
11 changes: 6 additions & 5 deletions src/app/api/stores/[id]/staff/accept-invite/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ export async function POST(
const ownerMembership = await prisma.membership.findFirst({
where: {
role: 'OWNER',
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
},
select: {
userId: true,
user: { select: { name: true, email: true } },
include: {
user: {
select: { name: true, email: true },
},
},
});

Expand Down Expand Up @@ -169,7 +170,7 @@ export async function DELETE(
const ownerMembership = await prisma.membership.findFirst({
where: {
role: 'OWNER',
organization: { store: { id: storeId } },
organization: { stores: { some: { id: storeId } } },
},
select: { userId: true },
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/stores/[id]/staff/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<{ hasA
userId,
role: 'OWNER',
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
});
Expand Down Expand Up @@ -68,7 +68,7 @@ async function checkStoreAccess(userId: string, storeId: string): Promise<{ hasA
where: {
userId,
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
});
Expand Down
12 changes: 8 additions & 4 deletions src/app/api/subscriptions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export async function GET(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId,
stores: {
some: {
id: storeId,
},
},
},
},
Expand Down Expand Up @@ -162,8 +164,10 @@ export async function POST(request: NextRequest) {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId,
stores: {
some: {
id: storeId,
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/stores/[storeId]/roles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function StoreRolesPage({ params, searchParams }: PageProps
userId: session.user.id,
role: 'OWNER',
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
});
Expand Down
9 changes: 5 additions & 4 deletions src/app/dashboard/stores/[storeId]/roles/request/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,26 @@ export default async function RequestRolePage({ params }: PageProps) {
userId: session.user.id,
role: 'OWNER',
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
include: {
organization: {
include: {
store: {
stores: {
where: { id: storeId },
select: { id: true, name: true },
},
},
},
},
});

if (!ownerMembership?.organization?.store) {
if (!ownerMembership?.organization?.stores?.[0]) {
notFound();
}

const store = ownerMembership.organization.store;
const store = ownerMembership.organization.stores[0];

// Check existing custom roles count
const existingRolesCount = await prisma.customRole.count({
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/stores/[storeId]/staff/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default async function StoreStaffPage({ params, searchParams }: PageProps
userId: session.user.id,
role: 'OWNER',
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/stores/[storeId]/staff/staff-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function StaffList({
where: {
role: 'OWNER',
organization: {
store: { id: storeId }
stores: { some: { id: storeId } }
}
},
include: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/auth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function getUserContext(): Promise<UserContext | null> {
include: {
organization: {
include: {
store: true,
stores: true,
},
},
},
Expand Down Expand Up @@ -107,7 +107,7 @@ export async function getUserContext(): Promise<UserContext | null> {
const organizationRole = membership?.role;
const organizationId = membership?.organizationId;
const storeRole = storeStaff?.role;
const storeId = storeStaff?.storeId || membership?.organization?.store?.id;
const storeId = storeStaff?.storeId || membership?.organization?.stores?.[0]?.id;

// Determine effective role (highest level role takes precedence)
let effectiveRole: Role | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const authOptions: NextAuthOptions = {
session.user.organizationRole = membership?.role ?? undefined;
session.user.organizationId = membership?.organizationId ?? undefined;
session.user.storeRole = storeStaff?.role ?? undefined;
session.user.storeId = storeStaff?.storeId || membership?.organization?.store?.id;
session.user.storeId = storeStaff?.storeId || membership?.organization?.stores?.[0]?.id;

// Compute permissions
const { getPermissions } = await import('./permissions');
Expand Down
14 changes: 8 additions & 6 deletions src/lib/get-current-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function getCurrentStoreId(): Promise<string | null> {
include: {
organization: {
include: {
store: true,
stores: true,
},
},
},
Expand All @@ -57,7 +57,7 @@ export async function getCurrentStoreId(): Promise<string | null> {
});

// Return store ID if organization has a store
return membership?.organization?.store?.id || null;
return membership?.organization?.stores?.[0]?.id || null;
}

/**
Expand All @@ -79,7 +79,7 @@ export async function getCurrentStore() {
include: {
organization: {
include: {
store: true,
stores: true,
},
},
},
Expand All @@ -88,7 +88,7 @@ export async function getCurrentStore() {
},
});

return membership?.organization?.store || null;
return membership?.organization?.stores?.[0] || null;
}

/**
Expand Down Expand Up @@ -165,8 +165,10 @@ export async function verifyStoreAccess(storeId: string): Promise<boolean> {
where: {
userId: session.user.id,
organization: {
store: {
id: storeId,
stores: {
some: {
id: storeId,
},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/lib/services/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export const UpdateStoreSchema = z.object({
}).optional(),
}).transform((data) => {
// Create new object with only defined fields (not undefined)
const result: Record<string, any> = {};
const result: Record<string, unknown> = {};

// Copy all fields except domain and settings, but only if they're not undefined
Object.keys(data).forEach(key => {
if (key !== 'domain' && key !== 'settings') {
const value = (data as any)[key];
const value = data[key as keyof typeof data];
if (value !== undefined) {
result[key] = value;
}
Expand Down