task f.3
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { submitForApproval } from '@/features/foundation/approval/server/service';
|
||||
import { submitQuotationForApproval } from '@/features/crm/approval/server/service';
|
||||
import { buildCrmSecurityContext } from '@/features/crm/security/server/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
@@ -15,20 +16,26 @@ const submitSchema = z.object({
|
||||
export async function POST(request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
const { organization, session, membership } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalSubmit
|
||||
});
|
||||
const payload = submitSchema.parse(await request.json());
|
||||
await submitForApproval(organization.id, session.user.id, {
|
||||
workflowCode: 'quotation_standard_approval',
|
||||
entityType: 'quotation',
|
||||
entityId: id,
|
||||
remark: payload.remark
|
||||
|
||||
await submitQuotationForApproval({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
quotationId: id,
|
||||
remark: payload.remark,
|
||||
accessContext: buildCrmSecurityContext({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
membership
|
||||
})
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Quotation submitted for approval successfully'
|
||||
message: 'Quotation submitted approval successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
@@ -43,6 +50,6 @@ export async function POST(request: NextRequest, { params }: Params) {
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to submit quotation for approval' }, { status: 500 });
|
||||
return NextResponse.json({ message: 'Unable submit quotation for approval' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
119
src/app/api/crm/settings/approval-matrices/[id]/route.ts
Normal file
119
src/app/api/crm/settings/approval-matrices/[id]/route.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
updateApprovalMatrixSchema,
|
||||
type UpdateApprovalMatrixInput
|
||||
} from '@/features/crm/approval/schemas/approval-matrix.schema';
|
||||
import {
|
||||
getApprovalMatrix,
|
||||
softDeleteApprovalMatrix,
|
||||
updateApprovalMatrix
|
||||
} from '@/features/crm/approval/server/service';
|
||||
import { auditAction } from '@/features/foundation/audit-log/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
type Params = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalMatrixRead
|
||||
});
|
||||
const matrix = await getApprovalMatrix(id, organization.id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
time: new Date().toISOString(),
|
||||
message: 'Approval matrix loaded successfully',
|
||||
matrix
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to load approval matrix' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalMatrixUpdate
|
||||
});
|
||||
const payload = updateApprovalMatrixSchema.parse(
|
||||
await request.json()
|
||||
) as UpdateApprovalMatrixInput;
|
||||
const before = await getApprovalMatrix(id, organization.id);
|
||||
const updated = await updateApprovalMatrix(id, organization.id, session.user.id, payload);
|
||||
|
||||
await auditAction({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
entityType: 'crm_approval_matrix',
|
||||
entityId: id,
|
||||
action: 'update_approval_matrix',
|
||||
beforeData: before,
|
||||
afterData: updated
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Approval matrix updated successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ message: error.issues[0]?.message ?? 'Invalid payload' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to update approval matrix' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalMatrixDelete
|
||||
});
|
||||
const result = await softDeleteApprovalMatrix(id, organization.id, session.user.id);
|
||||
|
||||
await auditAction({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
entityType: 'crm_approval_matrix',
|
||||
entityId: id,
|
||||
action: 'delete_approval_matrix',
|
||||
beforeData: result.before,
|
||||
afterData: result.after
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Approval matrix deleted successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to delete approval matrix' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
77
src/app/api/crm/settings/approval-matrices/route.ts
Normal file
77
src/app/api/crm/settings/approval-matrices/route.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
createApprovalMatrixSchema,
|
||||
type CreateApprovalMatrixInput
|
||||
} from '@/features/crm/approval/schemas/approval-matrix.schema';
|
||||
import {
|
||||
createApprovalMatrix,
|
||||
listApprovalMatrices
|
||||
} from '@/features/crm/approval/server/service';
|
||||
import { auditAction } from '@/features/foundation/audit-log/service';
|
||||
import { PERMISSIONS } from '@/lib/auth/rbac';
|
||||
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
|
||||
|
||||
export async function GET(_request: NextRequest) {
|
||||
try {
|
||||
const { organization } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalMatrixRead
|
||||
});
|
||||
const items = await listApprovalMatrices(organization.id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
time: new Date().toISOString(),
|
||||
message: 'Approval matrices loaded successfully',
|
||||
items
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to load approval matrices' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { organization, session } = await requireOrganizationAccess({
|
||||
permission: PERMISSIONS.crmApprovalMatrixCreate
|
||||
});
|
||||
const payload = createApprovalMatrixSchema.parse(
|
||||
await request.json()
|
||||
) as CreateApprovalMatrixInput;
|
||||
const created = await createApprovalMatrix(organization.id, session.user.id, payload);
|
||||
|
||||
await auditAction({
|
||||
organizationId: organization.id,
|
||||
userId: session.user.id,
|
||||
entityType: 'crm_approval_matrix',
|
||||
entityId: created.id,
|
||||
action: 'create_approval_matrix',
|
||||
afterData: created
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Approval matrix created successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
return NextResponse.json({ message: error.message }, { status: error.status });
|
||||
}
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ message: error.issues[0]?.message ?? 'Invalid payload' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return NextResponse.json({ message: error.message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ message: 'Unable to create approval matrix' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user