task-d.5.7
This commit is contained in:
@@ -234,8 +234,15 @@ export interface QuotationRelationItem {
|
||||
id: string;
|
||||
code: string;
|
||||
status: string;
|
||||
revision: number;
|
||||
quotationType: string;
|
||||
totalAmount: number;
|
||||
currency: string;
|
||||
validUntil: string | null;
|
||||
approvedAt: string | null;
|
||||
acceptedAt: string | null;
|
||||
rejectedAt: string | null;
|
||||
rejectionReason: string | null;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -420,4 +427,3 @@ export interface MutationSuccessResponse {
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { and, eq, isNull } from 'drizzle-orm';
|
||||
import { crmOpportunities, crmQuotations } from '@/db/schema';
|
||||
import { auditAction } from '@/features/foundation/audit-log/service';
|
||||
import { db } from '@/lib/db';
|
||||
import { AuthError } from '@/lib/auth/session';
|
||||
|
||||
export async function assertOpportunityLinkIntegrity(args: {
|
||||
organizationId: string;
|
||||
opportunityId: string | null | undefined;
|
||||
customerId: string;
|
||||
productType?: string | null;
|
||||
}) {
|
||||
if (!args.opportunityId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [opportunity] = await db
|
||||
.select()
|
||||
.from(crmOpportunities)
|
||||
.where(
|
||||
and(
|
||||
eq(crmOpportunities.id, args.opportunityId),
|
||||
eq(crmOpportunities.organizationId, args.organizationId),
|
||||
isNull(crmOpportunities.deletedAt)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (!opportunity) {
|
||||
throw new AuthError('Opportunity not found', 404);
|
||||
}
|
||||
|
||||
if ((opportunity.outcomeStatus ?? 'open') !== 'open') {
|
||||
throw new AuthError('Closed opportunity cannot create quotation', 400);
|
||||
}
|
||||
|
||||
if (opportunity.customerId !== args.customerId) {
|
||||
throw new AuthError('Quotation customer must match linked opportunity customer', 400);
|
||||
}
|
||||
|
||||
if (args.productType && opportunity.productType !== args.productType) {
|
||||
throw new AuthError('Quotation product type must match linked opportunity product type', 400);
|
||||
}
|
||||
|
||||
return opportunity;
|
||||
}
|
||||
|
||||
export async function assertRevisionOpportunityLinkIntegrity(args: {
|
||||
organizationId: string;
|
||||
quotationId: string;
|
||||
}) {
|
||||
const [quotation] = await db
|
||||
.select()
|
||||
.from(crmQuotations)
|
||||
.where(
|
||||
and(
|
||||
eq(crmQuotations.id, args.quotationId),
|
||||
eq(crmQuotations.organizationId, args.organizationId),
|
||||
isNull(crmQuotations.deletedAt)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (!quotation) {
|
||||
throw new AuthError('Quotation not found', 404);
|
||||
}
|
||||
|
||||
return quotation.opportunityId ?? null;
|
||||
}
|
||||
|
||||
export async function auditQuotationOpportunityLink(args: {
|
||||
organizationId: string;
|
||||
userId: string;
|
||||
quotationId: string;
|
||||
opportunityId: string | null;
|
||||
previousOpportunityId?: string | null;
|
||||
reason: string;
|
||||
}) {
|
||||
if (!args.opportunityId && !args.previousOpportunityId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await auditAction({
|
||||
organizationId: args.organizationId,
|
||||
userId: args.userId,
|
||||
entityType: 'crm_quotation',
|
||||
entityId: args.quotationId,
|
||||
action: 'sync_quotation_opportunity_link',
|
||||
afterData: {
|
||||
quotationId: args.quotationId,
|
||||
opportunityId: args.opportunityId,
|
||||
previousOpportunityId: args.previousOpportunityId ?? null,
|
||||
reason: args.reason,
|
||||
actedBy: args.userId
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -26,6 +26,15 @@ import {
|
||||
import { generateNextDocumentCode } from '@/features/foundation/document-sequence/service';
|
||||
import { getActiveOptionsByCategory } from '@/features/foundation/master-options/service';
|
||||
import { syncOpportunityPipelineStageFromQuotationStatus } from '@/features/crm/opportunities/server/service';
|
||||
import {
|
||||
syncOpportunityFromQuotationApproval,
|
||||
syncOpportunityFromQuotationCreated
|
||||
} from '@/features/crm/opportunities/server/quotation-sync.service';
|
||||
import {
|
||||
assertOpportunityLinkIntegrity,
|
||||
assertRevisionOpportunityLinkIntegrity,
|
||||
auditQuotationOpportunityLink
|
||||
} from './opportunity-link.service';
|
||||
import type {
|
||||
QuotationActivityRecord,
|
||||
QuotationAttachmentMutationPayload,
|
||||
@@ -1297,12 +1306,11 @@ export async function createQuotation(
|
||||
payload.status
|
||||
);
|
||||
|
||||
const opportunity = payload.opportunityId
|
||||
? await assertOpportunityBelongsToOrganization(payload.opportunityId, organizationId)
|
||||
: null;
|
||||
if (opportunity) {
|
||||
assertOpportunityOpenForQuotation(opportunity);
|
||||
}
|
||||
const opportunity = await assertOpportunityLinkIntegrity({
|
||||
organizationId,
|
||||
opportunityId: payload.opportunityId,
|
||||
customerId: payload.customerId
|
||||
});
|
||||
const documentCode = await generateNextDocumentCode({
|
||||
organizationId,
|
||||
documentType: 'quotation',
|
||||
@@ -1362,6 +1370,19 @@ export async function createQuotation(
|
||||
);
|
||||
|
||||
if (created.opportunityId) {
|
||||
await auditQuotationOpportunityLink({
|
||||
organizationId,
|
||||
userId,
|
||||
quotationId: created.id,
|
||||
opportunityId: created.opportunityId,
|
||||
reason: 'quotation_created'
|
||||
});
|
||||
await syncOpportunityFromQuotationCreated({
|
||||
organizationId,
|
||||
userId,
|
||||
opportunityId: created.opportunityId,
|
||||
quotationId: created.id
|
||||
});
|
||||
await syncOpportunityPipelineStageFromQuotationStatus(
|
||||
created.opportunityId,
|
||||
organizationId,
|
||||
@@ -1387,9 +1408,11 @@ export async function updateQuotation(
|
||||
payload.status
|
||||
);
|
||||
const existing = await assertQuotationBelongsToOrganization(id, organizationId, accessContext);
|
||||
const opportunity = payload.opportunityId
|
||||
? await assertOpportunityBelongsToOrganization(payload.opportunityId, organizationId)
|
||||
: null;
|
||||
const opportunity = await assertOpportunityLinkIntegrity({
|
||||
organizationId,
|
||||
opportunityId: payload.opportunityId,
|
||||
customerId: payload.customerId
|
||||
});
|
||||
|
||||
const updated = await db.transaction(async (tx) => {
|
||||
const [row] = await tx
|
||||
@@ -1435,11 +1458,26 @@ export async function updateQuotation(
|
||||
);
|
||||
|
||||
if (row.opportunityId) {
|
||||
await auditQuotationOpportunityLink({
|
||||
organizationId,
|
||||
userId,
|
||||
quotationId: row.id,
|
||||
opportunityId: row.opportunityId,
|
||||
previousOpportunityId: existing.opportunityId,
|
||||
reason: 'quotation_updated'
|
||||
});
|
||||
await syncOpportunityPipelineStageFromQuotationStatus(
|
||||
row.opportunityId,
|
||||
organizationId,
|
||||
quotationStatusCode
|
||||
);
|
||||
await syncOpportunityFromQuotationApproval({
|
||||
organizationId,
|
||||
userId,
|
||||
opportunityId: row.opportunityId,
|
||||
quotationId: row.id,
|
||||
quotationStatusCode
|
||||
});
|
||||
}
|
||||
|
||||
return row;
|
||||
@@ -2125,6 +2163,7 @@ export async function createQuotationRevision(
|
||||
revisionRemark?: string
|
||||
) {
|
||||
const parent = await assertQuotationBelongsToOrganization(quotationId, organizationId);
|
||||
await assertRevisionOpportunityLinkIntegrity({ organizationId, quotationId });
|
||||
const statusCode = await resolveOptionCodeById(
|
||||
organizationId,
|
||||
QUOTATION_OPTION_CATEGORIES.status,
|
||||
@@ -2273,6 +2312,23 @@ export async function createQuotationRevision(
|
||||
}
|
||||
}
|
||||
|
||||
if (created.opportunityId) {
|
||||
await auditQuotationOpportunityLink({
|
||||
organizationId,
|
||||
userId,
|
||||
quotationId: created.id,
|
||||
opportunityId: created.opportunityId,
|
||||
previousOpportunityId: parent.opportunityId,
|
||||
reason: 'quotation_revision_created'
|
||||
});
|
||||
await syncOpportunityFromQuotationCreated({
|
||||
organizationId,
|
||||
userId,
|
||||
opportunityId: created.opportunityId,
|
||||
quotationId: created.id
|
||||
});
|
||||
}
|
||||
|
||||
return created;
|
||||
});
|
||||
}
|
||||
@@ -2292,14 +2348,21 @@ export async function listQuotationRevisions(id: string, organizationId: string)
|
||||
)
|
||||
.orderBy(asc(crmQuotations.revision), asc(crmQuotations.createdAt));
|
||||
|
||||
return rows.map<QuotationRelationItem>((row) => ({
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
status: row.status,
|
||||
quotationType: row.quotationType,
|
||||
totalAmount: row.totalAmount,
|
||||
updatedAt: row.updatedAt.toISOString()
|
||||
}));
|
||||
return rows.map<QuotationRelationItem>((row) => ({
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
status: row.status,
|
||||
revision: row.revision,
|
||||
quotationType: row.quotationType,
|
||||
totalAmount: row.totalAmount,
|
||||
currency: row.currency,
|
||||
validUntil: row.validUntil?.toISOString() ?? null,
|
||||
approvedAt: row.approvedAt?.toISOString() ?? null,
|
||||
acceptedAt: row.acceptedAt?.toISOString() ?? null,
|
||||
rejectedAt: row.rejectedAt?.toISOString() ?? null,
|
||||
rejectionReason: row.rejectionReason,
|
||||
updatedAt: row.updatedAt.toISOString()
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listOpportunityQuotationRelations(
|
||||
@@ -2318,14 +2381,21 @@ export async function listOpportunityQuotationRelations(
|
||||
)
|
||||
.orderBy(desc(crmQuotations.updatedAt));
|
||||
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
status: row.status,
|
||||
quotationType: row.quotationType,
|
||||
totalAmount: row.totalAmount,
|
||||
updatedAt: row.updatedAt.toISOString()
|
||||
}));
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
status: row.status,
|
||||
revision: row.revision,
|
||||
quotationType: row.quotationType,
|
||||
totalAmount: row.totalAmount,
|
||||
currency: row.currency,
|
||||
validUntil: row.validUntil?.toISOString() ?? null,
|
||||
approvedAt: row.approvedAt?.toISOString() ?? null,
|
||||
acceptedAt: row.acceptedAt?.toISOString() ?? null,
|
||||
rejectedAt: row.rejectedAt?.toISOString() ?? null,
|
||||
rejectionReason: row.rejectionReason,
|
||||
updatedAt: row.updatedAt.toISOString()
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listCustomerQuotationRelations(
|
||||
@@ -2351,8 +2421,15 @@ export async function listCustomerQuotationRelations(
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
status: row.status,
|
||||
revision: row.revision,
|
||||
quotationType: row.quotationType,
|
||||
totalAmount: row.totalAmount,
|
||||
currency: row.currency,
|
||||
validUntil: row.validUntil?.toISOString() ?? null,
|
||||
approvedAt: row.approvedAt?.toISOString() ?? null,
|
||||
acceptedAt: row.acceptedAt?.toISOString() ?? null,
|
||||
rejectedAt: row.rejectedAt?.toISOString() ?? null,
|
||||
rejectionReason: row.rejectionReason,
|
||||
updatedAt: row.updatedAt.toISOString()
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user