feat(tenders): add Tender Management module (SRS, backend, frontend)

- SRS document: docs/SRS_TENDER_MANAGEMENT.md
- Prisma: Tender, TenderDirective models; Deal.sourceTenderId; Attachment.tenderId/tenderDirectiveId
- Backend: tenders module (CRUD, duplicate check, directives, notifications, file upload, convert-to-deal)
- Frontend: tenders list, detail, create/edit forms, directives, convert to deal, i18n (en/ar), dashboard card
- Seed: tenders permissions for admin and sales positions
- Auth: admin.service findFirst for email check (Prisma compatibility)

Made-with: Cursor
This commit is contained in:
Talal Sharabi
2026-03-11 16:57:40 +04:00
parent 18c13cdf7c
commit 4c139429e2
14 changed files with 2623 additions and 17 deletions

View File

@@ -0,0 +1,528 @@
'use client'
import { useState, useEffect, useRef } from 'react'
import { useParams, useRouter } from 'next/navigation'
import Link from 'next/link'
import { toast } from 'react-hot-toast'
import {
ArrowLeft,
FileText,
Calendar,
Building2,
DollarSign,
User,
History,
Plus,
Loader2,
CheckCircle2,
Upload,
ExternalLink,
AlertCircle,
} from 'lucide-react'
import ProtectedRoute from '@/components/ProtectedRoute'
import LoadingSpinner from '@/components/LoadingSpinner'
import Modal from '@/components/Modal'
import { tendersAPI, Tender, TenderDirective, CreateDirectiveData } from '@/lib/api/tenders'
import { contactsAPI } from '@/lib/api/contacts'
import { pipelinesAPI } from '@/lib/api/pipelines'
import { employeesAPI } from '@/lib/api/employees'
import { useLanguage } from '@/contexts/LanguageContext'
const DIRECTIVE_TYPE_LABELS: Record<string, string> = {
BUY_TERMS: 'Buy terms booklet',
VISIT_CLIENT: 'Visit client',
MEET_COMMITTEE: 'Meet committee',
PREPARE_TO_BID: 'Prepare to bid',
}
function TenderDetailContent() {
const params = useParams()
const router = useRouter()
const tenderId = params.id as string
const { t } = useLanguage()
const [tender, setTender] = useState<Tender | null>(null)
const [history, setHistory] = useState<any[]>([])
const [loading, setLoading] = useState(true)
const [activeTab, setActiveTab] = useState<'info' | 'directives' | 'attachments' | 'history'>('info')
const [showDirectiveModal, setShowDirectiveModal] = useState(false)
const [showConvertModal, setShowConvertModal] = useState(false)
const [showCompleteModal, setShowCompleteModal] = useState<TenderDirective | null>(null)
const [employees, setEmployees] = useState<any[]>([])
const [contacts, setContacts] = useState<any[]>([])
const [pipelines, setPipelines] = useState<any[]>([])
const [directiveForm, setDirectiveForm] = useState<CreateDirectiveData>({ type: 'BUY_TERMS', assignedToEmployeeId: '', notes: '' })
const [convertForm, setConvertForm] = useState({ contactId: '', pipelineId: '', ownerId: '' })
const [completeNotes, setCompleteNotes] = useState('')
const [directiveTypeValues, setDirectiveTypeValues] = useState<string[]>([])
const [submitting, setSubmitting] = useState(false)
const fileInputRef = useRef<HTMLInputElement>(null)
const directiveFileInputRef = useRef<HTMLInputElement>(null)
const [uploadingDirectiveId, setUploadingDirectiveId] = useState<string | null>(null)
const [directiveIdForUpload, setDirectiveIdForUpload] = useState<string | null>(null)
const fetchTender = async () => {
try {
const data = await tendersAPI.getById(tenderId)
setTender(data)
} catch {
toast.error(t('tenders.loadError'))
} finally {
setLoading(false)
}
}
const fetchHistory = async () => {
try {
const data = await tendersAPI.getHistory(tenderId)
setHistory(data)
} catch {}
}
useEffect(() => {
fetchTender()
}, [tenderId])
useEffect(() => {
if (tender) fetchHistory()
}, [tender?.id])
useEffect(() => {
tendersAPI.getDirectiveTypeValues().then(setDirectiveTypeValues).catch(() => {})
}, [])
useEffect(() => {
if (showDirectiveModal || showConvertModal) {
employeesAPI.getAll({ status: 'ACTIVE', pageSize: 500 }).then((r: any) => setEmployees(r.employees || [])).catch(() => {})
}
if (showConvertModal) {
contactsAPI.getAll({ pageSize: 500 }).then((r: any) => setContacts(r.contacts || [])).catch(() => {})
pipelinesAPI.getAll().then(setPipelines).catch(() => {})
}
}, [showDirectiveModal, showConvertModal])
const handleAddDirective = async (e: React.FormEvent) => {
e.preventDefault()
if (!directiveForm.assignedToEmployeeId) {
toast.error(t('tenders.assignee') + ' ' + t('common.required'))
return
}
setSubmitting(true)
try {
await tendersAPI.createDirective(tenderId, directiveForm)
toast.success('Directive created')
setShowDirectiveModal(false)
setDirectiveForm({ type: 'BUY_TERMS', assignedToEmployeeId: '', notes: '' })
fetchTender()
} catch (err: any) {
toast.error(err.response?.data?.message || 'Failed')
} finally {
setSubmitting(false)
}
}
const handleCompleteDirective = async (e: React.FormEvent) => {
e.preventDefault()
if (!showCompleteModal) return
setSubmitting(true)
try {
await tendersAPI.updateDirective(showCompleteModal.id, { status: 'COMPLETED', completionNotes: completeNotes })
toast.success('Task completed')
setShowCompleteModal(null)
setCompleteNotes('')
fetchTender()
} catch (err: any) {
toast.error(err.response?.data?.message || 'Failed')
} finally {
setSubmitting(false)
}
}
const handleConvertToDeal = async (e: React.FormEvent) => {
e.preventDefault()
if (!convertForm.contactId || !convertForm.pipelineId) {
toast.error('Contact and Pipeline are required')
return
}
setSubmitting(true)
try {
const deal = await tendersAPI.convertToDeal(tenderId, convertForm)
toast.success('Tender converted to deal')
setShowConvertModal(false)
router.push(`/crm/deals/${deal.id}`)
} catch (err: any) {
toast.error(err.response?.data?.message || 'Failed')
} finally {
setSubmitting(false)
}
}
const handleTenderFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
if (!file) return
setSubmitting(true)
try {
await tendersAPI.uploadTenderAttachment(tenderId, file)
toast.success(t('tenders.uploadFile'))
fetchTender()
} catch (err: any) {
toast.error(err.response?.data?.message || 'Upload failed')
} finally {
setSubmitting(false)
e.target.value = ''
}
}
const handleDirectiveFileSelect = (directiveId: string) => {
setDirectiveIdForUpload(directiveId)
setTimeout(() => directiveFileInputRef.current?.click(), 0)
}
const handleDirectiveFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
const directiveId = directiveIdForUpload
e.target.value = ''
setDirectiveIdForUpload(null)
if (!file || !directiveId) return
setUploadingDirectiveId(directiveId)
try {
await tendersAPI.uploadDirectiveAttachment(directiveId, file)
toast.success(t('tenders.uploadFile'))
fetchTender()
} catch (err: any) {
toast.error(err.response?.data?.message || 'Upload failed')
} finally {
setUploadingDirectiveId(null)
}
}
if (loading || !tender) {
return (
<div className="min-h-screen flex items-center justify-center">
<LoadingSpinner />
</div>
)
}
const tabs = [
{ id: 'info', label: t('tenders.titleLabel') || 'Info', icon: FileText },
{ id: 'directives', label: t('tenders.directives'), icon: CheckCircle2 },
{ id: 'attachments', label: t('tenders.attachments'), icon: Upload },
{ id: 'history', label: t('tenders.history'), icon: History },
]
return (
<div className="min-h-screen bg-gray-50">
<div className="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div className="mb-6 flex items-center justify-between">
<div className="flex items-center gap-3">
<Link href="/tenders" className="p-2 hover:bg-gray-200 rounded-lg">
<ArrowLeft className="h-5 w-5" />
</Link>
<div>
<h1 className="text-2xl font-bold text-gray-900">{tender.tenderNumber} {tender.title}</h1>
<p className="text-sm text-gray-600">{tender.issuingBodyName}</p>
</div>
</div>
{tender.status === 'ACTIVE' && (
<button
onClick={() => setShowConvertModal(true)}
className="flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700"
>
<ExternalLink className="h-4 w-4" />
{t('tenders.convertToDeal')}
</button>
)}
</div>
<div className="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
<div className="border-b border-gray-200 flex gap-1 p-1">
{tabs.map((tab) => (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id as any)}
className={`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium ${
activeTab === tab.id ? 'bg-indigo-100 text-indigo-800' : 'text-gray-600 hover:bg-gray-100'
}`}
>
<tab.icon className="h-4 w-4" />
{tab.label}
</button>
))}
</div>
<div className="p-6">
{activeTab === 'info' && (
<div className="space-y-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="flex items-start gap-2">
<Calendar className="h-5 w-5 text-gray-400 mt-0.5" />
<div>
<p className="text-xs text-gray-500">{t('tenders.announcementDate')}</p>
<p>{tender.announcementDate?.split('T')[0]}</p>
</div>
</div>
<div className="flex items-start gap-2">
<Calendar className="h-5 w-5 text-gray-400 mt-0.5" />
<div>
<p className="text-xs text-gray-500">{t('tenders.closingDate')}</p>
<p>{tender.closingDate?.split('T')[0]}</p>
</div>
</div>
<div className="flex items-start gap-2">
<DollarSign className="h-5 w-5 text-gray-400 mt-0.5" />
<div>
<p className="text-xs text-gray-500">{t('tenders.termsValue')}</p>
<p>{Number(tender.termsValue)} SAR</p>
</div>
</div>
<div className="flex items-start gap-2">
<DollarSign className="h-5 w-5 text-gray-400 mt-0.5" />
<div>
<p className="text-xs text-gray-500">{t('tenders.bondValue')}</p>
<p>{Number(tender.bondValue)} SAR</p>
</div>
</div>
</div>
{tender.announcementLink && (
<p>
<a href={tender.announcementLink} target="_blank" rel="noopener noreferrer" className="text-indigo-600 hover:underline">
{t('tenders.announcementLink')}
</a>
</p>
)}
{tender.notes && (
<div>
<p className="text-xs text-gray-500">{t('common.notes')}</p>
<p className="whitespace-pre-wrap">{tender.notes}</p>
</div>
)}
</div>
)}
{activeTab === 'directives' && (
<div>
<div className="flex justify-between items-center mb-4">
<h3 className="font-medium">{t('tenders.directives')}</h3>
<button
onClick={() => setShowDirectiveModal(true)}
className="flex items-center gap-1 text-indigo-600 hover:underline"
>
<Plus className="h-4 w-4" />
{t('tenders.addDirective')}
</button>
</div>
{!tender.directives?.length ? (
<p className="text-gray-500">{t('common.noData')}</p>
) : (
<ul className="space-y-3">
{tender.directives.map((d) => (
<li key={d.id} className="border rounded-lg p-4 flex flex-wrap items-center justify-between gap-2">
<div>
<p className="font-medium">{DIRECTIVE_TYPE_LABELS[d.type] || d.type}</p>
<p className="text-sm text-gray-600">
{d.assignedToEmployee ? `${d.assignedToEmployee.firstName} ${d.assignedToEmployee.lastName}` : ''} · {d.status}
</p>
{d.completionNotes && <p className="text-sm mt-1">{d.completionNotes}</p>}
</div>
<div className="flex items-center gap-2">
{d.status !== 'COMPLETED' && d.assignedToEmployee?.user?.id && (
<button
onClick={() => setShowCompleteModal(d)}
className="text-sm text-green-600 hover:underline"
>
{t('tenders.completeTask')}
</button>
)}
<input
type="file"
ref={directiveFileInputRef}
className="hidden"
onChange={handleDirectiveFileUpload}
/>
<button
type="button"
onClick={() => handleDirectiveFileSelect(d.id)}
disabled={uploadingDirectiveId === d.id}
className="text-sm text-indigo-600 hover:underline flex items-center gap-1"
>
{uploadingDirectiveId === d.id ? <Loader2 className="h-4 w-4 animate-spin" /> : <Upload className="h-4 w-4" />}
{t('tenders.uploadFile')}
</button>
</div>
</li>
))}
</ul>
)}
</div>
)}
{activeTab === 'attachments' && (
<div>
<div className="flex items-center gap-4 mb-4">
<input
type="file"
ref={fileInputRef}
className="hidden"
onChange={handleTenderFileUpload}
/>
<button
onClick={() => fileInputRef.current?.click()}
disabled={submitting}
className="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 disabled:opacity-50"
>
{submitting ? <Loader2 className="h-4 w-4 animate-spin" /> : <Upload className="h-4 w-4" />}
{t('tenders.uploadFile')}
</button>
</div>
{!tender.attachments?.length ? (
<p className="text-gray-500">{t('common.noData')}</p>
) : (
<ul className="space-y-2">
{tender.attachments.map((a: any) => (
<li key={a.id} className="text-sm text-gray-700">
{a.originalName || a.fileName}
</li>
))}
</ul>
)}
</div>
)}
{activeTab === 'history' && (
<div>
{history.length === 0 ? (
<p className="text-gray-500">{t('common.noData')}</p>
) : (
<ul className="space-y-2">
{history.map((h: any) => (
<li key={h.id} className="text-sm border-b border-gray-100 pb-2">
<span className="font-medium">{h.action}</span> · {h.user?.username} · {h.createdAt?.split('T')[0]}
</li>
))}
</ul>
)}
</div>
)}
</div>
</div>
</div>
<Modal isOpen={showDirectiveModal} onClose={() => setShowDirectiveModal(false)} title={t('tenders.addDirective')}>
<form onSubmit={handleAddDirective} className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">{t('tenders.directiveType')}</label>
<select
value={directiveForm.type}
onChange={(e) => setDirectiveForm({ ...directiveForm, type: e.target.value })}
className="w-full px-3 py-2 border rounded-lg"
>
{directiveTypeValues.map((v) => (
<option key={v} value={v}>{DIRECTIVE_TYPE_LABELS[v] || v}</option>
))}
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">{t('tenders.assignee')} *</label>
<select
value={directiveForm.assignedToEmployeeId}
onChange={(e) => setDirectiveForm({ ...directiveForm, assignedToEmployeeId: e.target.value })}
className="w-full px-3 py-2 border rounded-lg"
required
>
<option value="">Select employee</option>
{employees.map((emp) => (
<option key={emp.id} value={emp.id}>{emp.firstName} {emp.lastName}</option>
))}
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">{t('common.notes')}</label>
<textarea
value={directiveForm.notes || ''}
onChange={(e) => setDirectiveForm({ ...directiveForm, notes: e.target.value })}
className="w-full px-3 py-2 border rounded-lg"
rows={2}
/>
</div>
<div className="flex justify-end gap-2">
<button type="button" onClick={() => setShowDirectiveModal(false)} className="px-4 py-2 border rounded-lg">{t('common.cancel')}</button>
<button type="submit" disabled={submitting} className="px-4 py-2 bg-indigo-600 text-white rounded-lg disabled:opacity-50 flex items-center gap-2">
{submitting && <Loader2 className="h-4 w-4 animate-spin" />}
{t('common.save')}
</button>
</div>
</form>
</Modal>
<Modal isOpen={!!showCompleteModal} onClose={() => setShowCompleteModal(null)} title={t('tenders.completeTask')}>
<form onSubmit={handleCompleteDirective} className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">{t('tenders.completionNotes')}</label>
<textarea
value={completeNotes}
onChange={(e) => setCompleteNotes(e.target.value)}
className="w-full px-3 py-2 border rounded-lg"
rows={3}
/>
</div>
<div className="flex justify-end gap-2">
<button type="button" onClick={() => setShowCompleteModal(null)} className="px-4 py-2 border rounded-lg">{t('common.cancel')}</button>
<button type="submit" disabled={submitting} className="px-4 py-2 bg-green-600 text-white rounded-lg disabled:opacity-50 flex items-center gap-2">
{submitting && <Loader2 className="h-4 w-4 animate-spin" />}
{t('common.save')}
</button>
</div>
</form>
</Modal>
<Modal isOpen={showConvertModal} onClose={() => setShowConvertModal(false)} title={t('tenders.convertToDeal')}>
<form onSubmit={handleConvertToDeal} className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">Contact *</label>
<select
value={convertForm.contactId}
onChange={(e) => setConvertForm({ ...convertForm, contactId: e.target.value })}
className="w-full px-3 py-2 border rounded-lg"
required
>
<option value="">Select contact</option>
{contacts.map((c) => (
<option key={c.id} value={c.id}>{c.name}</option>
))}
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">Pipeline *</label>
<select
value={convertForm.pipelineId}
onChange={(e) => setConvertForm({ ...convertForm, pipelineId: e.target.value })}
className="w-full px-3 py-2 border rounded-lg"
required
>
<option value="">Select pipeline</option>
{pipelines.map((p) => (
<option key={p.id} value={p.id}>{p.name}</option>
))}
</select>
</div>
<div className="flex justify-end gap-2">
<button type="button" onClick={() => setShowConvertModal(false)} className="px-4 py-2 border rounded-lg">{t('common.cancel')}</button>
<button type="submit" disabled={submitting} className="px-4 py-2 bg-green-600 text-white rounded-lg disabled:opacity-50 flex items-center gap-2">
{submitting && <Loader2 className="h-4 w-4 animate-spin" />}
{t('tenders.convertToDeal')}
</button>
</div>
</form>
</Modal>
</div>
)
}
export default function TenderDetailPage() {
return (
<ProtectedRoute>
<TenderDetailContent />
</ProtectedRoute>
)
}