forked from wangziqi/gongxue-base
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bfd30b513 | |||
| ceb0e2bf14 | |||
| acb11b87a3 | |||
| b9e4caae99 | |||
| f98c0ccaa8 | |||
| aa1ed7db56 | |||
| 0533c30ece | |||
| 1f32d1285b | |||
| 7b08560aef | |||
| 04ef5c42d9 | |||
| 0515e6ed27 | |||
| effa34434b | |||
| 0377acd33b | |||
| 6396d3e934 | |||
| dfd3cf6772 | |||
| 2874ab7bee | |||
| 8cadf8970e | |||
| 337d25e370 | |||
| 7860703672 | |||
| ebce2463f0 | |||
| 910a4263f6 | |||
| 6d42241918 | |||
| 70283d9dfe | |||
| 187e4f2af0 | |||
| 619b3b22bd | |||
| b5ef91bf2d |
@@ -16,7 +16,10 @@ instance.interceptors.request.use((config) => {
|
|||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
(res) => res.data,
|
(res) => res.data,
|
||||||
(err) => {
|
(err) => {
|
||||||
if (err.response?.status === 401) {
|
const isLoginRequest =
|
||||||
|
err.config?.url === '/auth/login' || err.config?.url === 'auth/login';
|
||||||
|
|
||||||
|
if (err.response?.status === 401 && !isLoginRequest) {
|
||||||
localStorage.removeItem('token');
|
localStorage.removeItem('token');
|
||||||
localStorage.removeItem('user');
|
localStorage.removeItem('user');
|
||||||
localStorage.removeItem('permissions');
|
localStorage.removeItem('permissions');
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Badge, Popover, Button, List, Typography, Empty } from 'antd';
|
|||||||
import { BellOutlined } from '@ant-design/icons';
|
import { BellOutlined } from '@ant-design/icons';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
|
import { formatNotificationText, notificationTypeLabels } from '../utils/notification-display';
|
||||||
|
|
||||||
interface NotificationItem {
|
interface NotificationItem {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -14,18 +15,6 @@ interface NotificationItem {
|
|||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const typeLabels: Record<string, string> = {
|
|
||||||
bill_generated: '账单',
|
|
||||||
bill_paid: '账单',
|
|
||||||
check_in: '入住',
|
|
||||||
check_out: '退宿',
|
|
||||||
deposit_due: '押金',
|
|
||||||
deposit_refunded: '押金',
|
|
||||||
class_change: '班级',
|
|
||||||
schedule_conflict: '排课',
|
|
||||||
announcement: '公告',
|
|
||||||
};
|
|
||||||
|
|
||||||
function timeAgo(dateStr: string): string {
|
function timeAgo(dateStr: string): string {
|
||||||
const diff = Date.now() - new Date(dateStr).getTime();
|
const diff = Date.now() - new Date(dateStr).getTime();
|
||||||
const mins = Math.floor(diff / 60000);
|
const mins = Math.floor(diff / 60000);
|
||||||
@@ -163,7 +152,7 @@ const NotificationBell: React.FC = () => {
|
|||||||
strong={!item.isRead}
|
strong={!item.isRead}
|
||||||
style={{ fontSize: 14 }}
|
style={{ fontSize: 14 }}
|
||||||
>
|
>
|
||||||
[{typeLabels[item.type] || item.type}] {item.title}
|
[{notificationTypeLabels[item.type] || item.type}] {formatNotificationText(item.title)}
|
||||||
</Typography.Text>
|
</Typography.Text>
|
||||||
}
|
}
|
||||||
description={
|
description={
|
||||||
|
|||||||
@@ -321,7 +321,11 @@ const EnrollmentsTab: React.FC<TabProps & { data: EnrollmentRecord[] }> = ({
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
dataSource={data}
|
dataSource={data}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 15 }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title="添加报读记录"
|
title="添加报读记录"
|
||||||
@@ -428,7 +432,11 @@ const ExamScoresTab: React.FC<TabProps & { data: ExamScoreRecord[]; enrollments:
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
dataSource={data}
|
dataSource={data}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 15 }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title="添加考试成绩"
|
title="添加考试成绩"
|
||||||
@@ -529,7 +537,11 @@ const LearningTab: React.FC<TabProps & { data: LearningRecord[] }> = ({ data, st
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
dataSource={data}
|
dataSource={data}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 15 }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title="添加学情记录"
|
title="添加学情记录"
|
||||||
@@ -712,7 +724,11 @@ const AttachmentsTab: React.FC<TabProps & { data: AttachmentRecord[] }> = ({ dat
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
dataSource={data}
|
dataSource={data}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 15 }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50],
|
||||||
|
}}
|
||||||
style={{ marginTop: 16 }}
|
style={{ marginTop: 16 }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -399,7 +399,12 @@ const BillsPage: React.FC = () => {
|
|||||||
dataSource={filteredBills}
|
dataSource={filteredBills}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys: selectedRows,
|
selectedRowKeys: selectedRows,
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ interface ClassStudent {
|
|||||||
studentName: string;
|
studentName: string;
|
||||||
studentNo: string;
|
studentNo: string;
|
||||||
joinDate: string;
|
joinDate: string;
|
||||||
|
leaveDate: string | null;
|
||||||
status: string;
|
status: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,6 +306,7 @@ const ClassDetailPage: React.FC = () => {
|
|||||||
{ title: '姓名', dataIndex: 'studentName' },
|
{ title: '姓名', dataIndex: 'studentName' },
|
||||||
{ title: '学号', dataIndex: 'studentNo' },
|
{ title: '学号', dataIndex: 'studentNo' },
|
||||||
{ title: '加入日期', dataIndex: 'joinDate' },
|
{ title: '加入日期', dataIndex: 'joinDate' },
|
||||||
|
{ title: '离班日期', dataIndex: 'leaveDate', render: (v: string | null) => v || '-' },
|
||||||
{
|
{
|
||||||
title: '状态',
|
title: '状态',
|
||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
@@ -316,11 +318,12 @@ const ClassDetailPage: React.FC = () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '操作',
|
title: '操作',
|
||||||
render: (_: unknown, r: ClassStudent) => (
|
render: (_: unknown, r: ClassStudent) =>
|
||||||
<Popconfirm title="确认移除?" onConfirm={() => handleRemoveStudent(r.studentId)}>
|
r.status === 'active' ? (
|
||||||
<PermissionButton permission="class:edit" size="small" danger>移除</PermissionButton>
|
<Popconfirm title="确认移除?" onConfirm={() => handleRemoveStudent(r.studentId)}>
|
||||||
</Popconfirm>
|
<PermissionButton permission="class:edit" size="small" danger>移除</PermissionButton>
|
||||||
),
|
</Popconfirm>
|
||||||
|
) : null,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -527,7 +530,11 @@ const ClassDetailPage: React.FC = () => {
|
|||||||
columns={studentColumns}
|
columns={studentColumns}
|
||||||
dataSource={students}
|
dataSource={students}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 20 }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title="添加学员"
|
title="添加学员"
|
||||||
@@ -571,7 +578,11 @@ const ClassDetailPage: React.FC = () => {
|
|||||||
columns={teacherColumns}
|
columns={teacherColumns}
|
||||||
dataSource={teachers}
|
dataSource={teachers}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 20 }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title="添加教师"
|
title="添加教师"
|
||||||
@@ -630,7 +641,11 @@ const ClassDetailPage: React.FC = () => {
|
|||||||
columns={scheduleColumns}
|
columns={scheduleColumns}
|
||||||
dataSource={schedules}
|
dataSource={schedules}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 20 }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -264,7 +264,11 @@ const ClassesPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
pagination={{ pageSize: 20 }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
}}
|
||||||
scroll={{ x: 1100 }}
|
scroll={{ x: 1100 }}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
Tooltip,
|
Tooltip,
|
||||||
Empty,
|
Empty,
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import { PlusOutlined, UploadOutlined, DeleteOutlined, FileTextOutlined } from '@ant-design/icons';
|
import { PlusOutlined, UploadOutlined, DeleteOutlined, FileTextOutlined, StopOutlined, CheckOutlined } from '@ant-design/icons';
|
||||||
import dayjs, { Dayjs } from 'dayjs';
|
import dayjs, { Dayjs } from 'dayjs';
|
||||||
import api from '../../api';
|
import api from '../../api';
|
||||||
import { downloadBlob } from '../../utils/download';
|
import { downloadBlob } from '../../utils/download';
|
||||||
@@ -39,6 +39,7 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
const [editing, setEditing] = useState<any>(null);
|
const [editing, setEditing] = useState<any>(null);
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
const [filterMonth, setFilterMonth] = useState<Dayjs | null>(null);
|
const [filterMonth, setFilterMonth] = useState<Dayjs | null>(null);
|
||||||
|
const [filterStatus, setFilterStatus] = useState<string | undefined>();
|
||||||
const [searchText, setSearchText] = useState('');
|
const [searchText, setSearchText] = useState('');
|
||||||
const [saving, setSaving] = useState(false);
|
const [saving, setSaving] = useState(false);
|
||||||
const [unavailableDates, setUnavailableDates] = useState<Set<string>>(new Set());
|
const [unavailableDates, setUnavailableDates] = useState<Set<string>>(new Set());
|
||||||
@@ -48,20 +49,22 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
const selectedClassroomId = Form.useWatch('classroomId', form);
|
const selectedClassroomId = Form.useWatch('classroomId', form);
|
||||||
|
|
||||||
const filteredData = useMemo(() => {
|
const filteredData = useMemo(() => {
|
||||||
if (!searchText) return data;
|
|
||||||
const s = searchText.toLowerCase();
|
|
||||||
return data.filter((r: any) => {
|
return data.filter((r: any) => {
|
||||||
|
if (filterStatus && r.effectiveStatus !== filterStatus) return false;
|
||||||
|
if (!searchText) return true;
|
||||||
|
const s = searchText.toLowerCase();
|
||||||
const matchClassroom = r.classroom?.name?.toLowerCase().includes(s);
|
const matchClassroom = r.classroom?.name?.toLowerCase().includes(s);
|
||||||
const matchOrganization = r.lesseeOrganization?.name?.toLowerCase().includes(s);
|
const matchOrganization = r.lesseeOrganization?.name?.toLowerCase().includes(s);
|
||||||
return matchClassroom || matchOrganization;
|
return matchClassroom || matchOrganization;
|
||||||
});
|
});
|
||||||
}, [data, searchText]);
|
}, [data, searchText, filterStatus]);
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const params: any = {};
|
const params: any = {};
|
||||||
if (filterMonth) params.month = filterMonth.format('YYYY-MM');
|
if (filterMonth) params.month = filterMonth.format('YYYY-MM');
|
||||||
|
params.includeEnded = true;
|
||||||
const res: any = await api.get('/classroom-rentals', { params });
|
const res: any = await api.get('/classroom-rentals', { params });
|
||||||
setData(res);
|
setData(res);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -215,6 +218,16 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRentalAction = async (id: number, action: 'cancel' | 'end') => {
|
||||||
|
try {
|
||||||
|
await api.put(`/classroom-rentals/${id}/${action}`);
|
||||||
|
message.success(action === 'cancel' ? '租赁已取消' : '租赁已结束');
|
||||||
|
fetchData();
|
||||||
|
} catch (e: any) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleDownloadContract = async (id: number, filename?: string) => {
|
const handleDownloadContract = async (id: number, filename?: string) => {
|
||||||
try {
|
try {
|
||||||
await downloadBlob(`/classroom-rentals/${id}/contract`, filename || `contract-${id}.pdf`);
|
await downloadBlob(`/classroom-rentals/${id}/contract`, filename || `contract-${id}.pdf`);
|
||||||
@@ -308,6 +321,19 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
width: 100,
|
width: 100,
|
||||||
render: (v: any) => (v ? `¥${v}` : '-'),
|
render: (v: any) => (v ? `¥${v}` : '-'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: '状态',
|
||||||
|
dataIndex: 'effectiveStatus',
|
||||||
|
width: 90,
|
||||||
|
render: (status: string) => {
|
||||||
|
const config: Record<string, { text: string; color: string }> = {
|
||||||
|
active: { text: '进行中', color: 'green' },
|
||||||
|
ended: { text: '已结束', color: 'default' },
|
||||||
|
cancelled: { text: '已取消', color: 'red' },
|
||||||
|
};
|
||||||
|
return <Tag color={config[status]?.color}>{config[status]?.text || status}</Tag>;
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: '合同',
|
title: '合同',
|
||||||
width: 120,
|
width: 120,
|
||||||
@@ -364,21 +390,30 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
width: 150,
|
width: 150,
|
||||||
render: (_: any, record: any) => (
|
render: (_: any, record: any) => (
|
||||||
<Space>
|
<Space>
|
||||||
<PermissionButton
|
{record.effectiveStatus === 'active' && (
|
||||||
permission="rental:edit"
|
<>
|
||||||
size="small"
|
<PermissionButton permission="rental:edit" size="small" onClick={() => openEdit(record)}>
|
||||||
onClick={() => openEdit(record)}
|
编辑
|
||||||
>
|
</PermissionButton>
|
||||||
编辑
|
<Popconfirm title="确定取消该租赁?" onConfirm={() => handleRentalAction(record.id, 'cancel')}>
|
||||||
</PermissionButton>
|
<PermissionButton permission="rental:edit" size="small" danger icon={<StopOutlined />}>
|
||||||
<Popconfirm
|
取消
|
||||||
title="确定删除该租赁订单?合同文件将一并删除。"
|
</PermissionButton>
|
||||||
onConfirm={() => handleDelete(record.id)}
|
</Popconfirm>
|
||||||
>
|
{!dayjs(record.startDate).isAfter(dayjs(), 'day') && (
|
||||||
<PermissionButton permission="rental:delete" size="small" danger>
|
<Popconfirm title="确定今天结束该租赁?" onConfirm={() => handleRentalAction(record.id, 'end')}>
|
||||||
删除
|
<PermissionButton permission="rental:edit" size="small" icon={<CheckOutlined />}>
|
||||||
</PermissionButton>
|
结束
|
||||||
</Popconfirm>
|
</PermissionButton>
|
||||||
|
</Popconfirm>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{record.effectiveStatus !== 'active' && (
|
||||||
|
<Popconfirm title="确定删除该租赁订单?合同文件将一并删除。" onConfirm={() => handleDelete(record.id)}>
|
||||||
|
<PermissionButton permission="rental:delete" size="small" danger>删除</PermissionButton>
|
||||||
|
</Popconfirm>
|
||||||
|
)}
|
||||||
</Space>
|
</Space>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
@@ -415,6 +450,18 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
allowClear
|
allowClear
|
||||||
format="YYYY-MM"
|
format="YYYY-MM"
|
||||||
/>
|
/>
|
||||||
|
<Select
|
||||||
|
placeholder="状态"
|
||||||
|
allowClear
|
||||||
|
style={{ width: 110 }}
|
||||||
|
value={filterStatus}
|
||||||
|
onChange={setFilterStatus}
|
||||||
|
options={[
|
||||||
|
{ value: 'active', label: '进行中' },
|
||||||
|
{ value: 'ended', label: '已结束' },
|
||||||
|
{ value: 'cancelled', label: '已取消' },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
</Space>
|
</Space>
|
||||||
<PermissionButton
|
<PermissionButton
|
||||||
permission="rental:create"
|
permission="rental:create"
|
||||||
@@ -436,7 +483,12 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
scroll={{ x: 1200 }}
|
scroll={{ x: 1200 }}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -459,7 +511,7 @@ const ClassroomRentalsPage: React.FC = () => {
|
|||||||
optionFilterProp="label"
|
optionFilterProp="label"
|
||||||
placeholder="选择教室"
|
placeholder="选择教室"
|
||||||
onChange={handleClassroomChange}
|
onChange={handleClassroomChange}
|
||||||
options={classrooms.map((c) => ({
|
options={classrooms.filter((c) => c.status === 'available').map((c) => ({
|
||||||
value: c.id,
|
value: c.id,
|
||||||
label: `${c.building ? c.building + ' · ' : ''}${c.name}(${c.roomType})`,
|
label: `${c.building ? c.building + ' · ' : ''}${c.name}(${c.roomType})`,
|
||||||
}))}
|
}))}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ const ClassroomsPage: React.FC = () => {
|
|||||||
const filteredData = useMemo(() => {
|
const filteredData = useMemo(() => {
|
||||||
let result = data;
|
let result = data;
|
||||||
if (searchText) { const s = searchText.toLowerCase(); result = result.filter((d: Record<string, unknown>) => (typeof d.name === 'string' && d.name.toLowerCase().includes(s)) || (typeof d.building === 'string' && d.building.toLowerCase().includes(s))); }
|
if (searchText) { const s = searchText.toLowerCase(); result = result.filter((d: Record<string, unknown>) => (typeof d.name === 'string' && d.name.toLowerCase().includes(s)) || (typeof d.building === 'string' && d.building.toLowerCase().includes(s))); }
|
||||||
if (filterStatus) result = result.filter((d: Record<string, unknown>) => d.status === filterStatus);
|
if (filterStatus) result = result.filter((d: Record<string, unknown>) => d.effectiveStatus === filterStatus);
|
||||||
return result;
|
return result;
|
||||||
}, [data, searchText, filterStatus]);
|
}, [data, searchText, filterStatus]);
|
||||||
|
|
||||||
@@ -157,11 +157,11 @@ const ClassroomsPage: React.FC = () => {
|
|||||||
{
|
{
|
||||||
title: '状态', width: 100,
|
title: '状态', width: 100,
|
||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
render: (s: string, record: { currentUsage?: CurrentUsage | null }) => {
|
render: (_s: string, record: { effectiveStatus?: string; status: string; currentUsage?: CurrentUsage | null }) => {
|
||||||
const effectiveStatus = record.currentUsage ? 'in_use' : s;
|
const effectiveStatus = record.effectiveStatus || record.status;
|
||||||
return (
|
return (
|
||||||
<Tooltip title={record.currentUsage ? `${record.currentUsage.title} (${record.currentUsage.startTime}-${record.currentUsage.endTime})` : undefined}>
|
<Tooltip title={record.currentUsage ? `${record.currentUsage.title} (${record.currentUsage.startTime}-${record.currentUsage.endTime})` : undefined}>
|
||||||
<Tag color={statusMap[effectiveStatus]?.color}>{statusMap[effectiveStatus]?.text || s}</Tag>
|
<Tag color={statusMap[effectiveStatus]?.color}>{statusMap[effectiveStatus]?.text || effectiveStatus}</Tag>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -228,7 +228,7 @@ const ClassroomsPage: React.FC = () => {
|
|||||||
if (!e.target.value) setSearchText('');
|
if (!e.target.value) setSearchText('');
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Select placeholder="状态" allowClear style={{ width: 110 }} value={filterStatus} onChange={setFilterStatus} options={[{value:'available',label:'可用'},{value:'in_use',label:'使用中'},{value:'reserved',label:'已预留'},{value:'maintenance',label:'维护中'}]} />
|
<Select placeholder="状态" allowClear style={{ width: 110 }} value={filterStatus} onChange={setFilterStatus} options={[{value:'available',label:'可用'},{value:'in_use',label:'使用中'},{value:'reserved',label:'已预留'},{value:'maintenance',label:'维护中'},{value:'archived',label:'已归档'}]} />
|
||||||
<Button
|
<Button
|
||||||
type={showArchived ? 'primary' : 'default'}
|
type={showArchived ? 'primary' : 'default'}
|
||||||
onClick={() => setShowArchived(!showArchived)}
|
onClick={() => setShowArchived(!showArchived)}
|
||||||
@@ -287,7 +287,12 @@ const ClassroomsPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title={editing ? '编辑教室' : '添加教室'}
|
title={editing ? '编辑教室' : '添加教室'}
|
||||||
@@ -323,6 +328,16 @@ const ClassroomsPage: React.FC = () => {
|
|||||||
<Form.Item name="capacity" label="容量">
|
<Form.Item name="capacity" label="容量">
|
||||||
<InputNumber min={1} max={500} style={{ width: '100%' }} />
|
<InputNumber min={1} max={500} style={{ width: '100%' }} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
{editing && (
|
||||||
|
<Form.Item name="status" label="基础状态">
|
||||||
|
<Select
|
||||||
|
options={[
|
||||||
|
{ value: 'available', label: '可用' },
|
||||||
|
{ value: 'maintenance', label: '维护中' },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
<Form.Item name="notes" label="备注">
|
<Form.Item name="notes" label="备注">
|
||||||
<Input.TextArea rows={2} />
|
<Input.TextArea rows={2} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { buildDepositStudentOption } from './deposit-student-option';
|
||||||
|
|
||||||
|
describe('deposit student option', () => {
|
||||||
|
it('uses the student number as the non-sensitive identifier', () => {
|
||||||
|
expect(
|
||||||
|
buildDepositStudentOption({ id: 23, name: '张三', studentNo: 'S2026001' }),
|
||||||
|
).toEqual({
|
||||||
|
value: 23,
|
||||||
|
label: '张三 (S2026001)',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to the internal id when the student number is missing', () => {
|
||||||
|
expect(buildDepositStudentOption({ id: 23, name: '张三', studentNo: null })).toEqual({
|
||||||
|
value: 23,
|
||||||
|
label: '张三 (#23)',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
10
apps/admin/src/pages/Deposits/deposit-student-option.ts
Normal file
10
apps/admin/src/pages/Deposits/deposit-student-option.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export interface DepositStudentLookup {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
studentNo?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const buildDepositStudentOption = (student: DepositStudentLookup) => ({
|
||||||
|
value: student.id,
|
||||||
|
label: `${student.name} (${student.studentNo || `#${student.id}`})`,
|
||||||
|
});
|
||||||
@@ -17,9 +17,9 @@ import {
|
|||||||
import { PlusOutlined, DeleteOutlined, DollarOutlined } from '@ant-design/icons';
|
import { PlusOutlined, DeleteOutlined, DollarOutlined } from '@ant-design/icons';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import api from '../../api';
|
import api from '../../api';
|
||||||
import { maskPhone, maskIdNumber } from '../../utils/sensitive';
|
|
||||||
import PermissionButton from '../../components/PermissionButton';
|
import PermissionButton from '../../components/PermissionButton';
|
||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
|
import { buildDepositStudentOption } from './deposit-student-option';
|
||||||
|
|
||||||
const statusMap: Record<string, { text: string; color: string }> = {
|
const statusMap: Record<string, { text: string; color: string }> = {
|
||||||
paid: { text: '已缴', color: 'green' },
|
paid: { text: '已缴', color: 'green' },
|
||||||
@@ -28,18 +28,15 @@ const statusMap: Record<string, { text: string; color: string }> = {
|
|||||||
deducted: { text: '已全扣', color: 'red' },
|
deducted: { text: '已全扣', color: 'red' },
|
||||||
};
|
};
|
||||||
|
|
||||||
const refundStatusMap: Record<string, { text: string; color: string }> = {
|
|
||||||
pending: { text: '历史退款处理中', color: 'orange' },
|
|
||||||
head_teacher_approved: { text: '历史退款处理中', color: 'blue' },
|
|
||||||
finance_approved: { text: '已退款', color: 'green' },
|
|
||||||
refunded: { text: '已退款', color: 'green' },
|
|
||||||
};
|
|
||||||
|
|
||||||
const installmentStatusMap: Record<string, { text: string; color: string }> = {
|
const installmentStatusMap: Record<string, { text: string; color: string }> = {
|
||||||
pending: { text: '待缴', color: 'orange' },
|
pending: { text: '待缴', color: 'orange' },
|
||||||
paid: { text: '已缴', color: 'green' },
|
paid: { text: '已缴', color: 'green' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isFormValidationError = (error: unknown) =>
|
||||||
|
typeof error === 'object'
|
||||||
|
&& error !== null
|
||||||
|
&& Array.isArray((error as { errorFields?: unknown }).errorFields);
|
||||||
|
|
||||||
const DepositsPage: React.FC = () => {
|
const DepositsPage: React.FC = () => {
|
||||||
const [data, setData] = useState<any[]>([]);
|
const [data, setData] = useState<any[]>([]);
|
||||||
@@ -92,17 +89,14 @@ const DepositsPage: React.FC = () => {
|
|||||||
() =>
|
() =>
|
||||||
students
|
students
|
||||||
.filter((s: any) => s.status === 'active')
|
.filter((s: any) => s.status === 'active')
|
||||||
.map((s: any) => ({
|
.map(buildDepositStudentOption),
|
||||||
value: s.id,
|
|
||||||
label: `${s.name} (${s.idNumber ? maskIdNumber(s.idNumber) : (s.phone ? maskPhone(s.phone) : '')})`,
|
|
||||||
})),
|
|
||||||
[students],
|
[students],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleCreate = async () => {
|
const handleCreate = async () => {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
const values = await createForm.validateFields();
|
|
||||||
try {
|
try {
|
||||||
|
const values = await createForm.validateFields();
|
||||||
await api.post('/deposits', {
|
await api.post('/deposits', {
|
||||||
studentId: values.studentId,
|
studentId: values.studentId,
|
||||||
amount: values.amount,
|
amount: values.amount,
|
||||||
@@ -114,7 +108,9 @@ const DepositsPage: React.FC = () => {
|
|||||||
createForm.resetFields();
|
createForm.resetFields();
|
||||||
fetchData();
|
fetchData();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.message || '操作失败');
|
if (!isFormValidationError(e)) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
}
|
}
|
||||||
@@ -122,8 +118,8 @@ const DepositsPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleRefund = async () => {
|
const handleRefund = async () => {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
const values = await refundForm.validateFields();
|
|
||||||
try {
|
try {
|
||||||
|
const values = await refundForm.validateFields();
|
||||||
await api.put(`/deposits/${refundModal.id}/refund`, {
|
await api.put(`/deposits/${refundModal.id}/refund`, {
|
||||||
refundDate: values.refundDate.format('YYYY-MM-DD'),
|
refundDate: values.refundDate.format('YYYY-MM-DD'),
|
||||||
deductionAmount: values.deductionAmount || 0,
|
deductionAmount: values.deductionAmount || 0,
|
||||||
@@ -135,7 +131,9 @@ const DepositsPage: React.FC = () => {
|
|||||||
refundForm.resetFields();
|
refundForm.resetFields();
|
||||||
fetchData();
|
fetchData();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.message || '操作失败');
|
if (!isFormValidationError(e)) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
}
|
}
|
||||||
@@ -143,8 +141,8 @@ const DepositsPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleAddInstallment = async () => {
|
const handleAddInstallment = async () => {
|
||||||
if (installmentModal == null) return;
|
if (installmentModal == null) return;
|
||||||
const values = await installmentForm.validateFields();
|
|
||||||
try {
|
try {
|
||||||
|
const values = await installmentForm.validateFields();
|
||||||
await api.post(`/deposits/${installmentModal}/installments`, {
|
await api.post(`/deposits/${installmentModal}/installments`, {
|
||||||
amount: values.amount,
|
amount: values.amount,
|
||||||
dueDate: values.dueDate.format('YYYY-MM-DD'),
|
dueDate: values.dueDate.format('YYYY-MM-DD'),
|
||||||
@@ -154,7 +152,9 @@ const DepositsPage: React.FC = () => {
|
|||||||
installmentForm.resetFields();
|
installmentForm.resetFields();
|
||||||
fetchData();
|
fetchData();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.message || '操作失败');
|
if (!isFormValidationError(e)) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -190,12 +190,6 @@ const DepositsPage: React.FC = () => {
|
|||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
render: (s: string) => <Tag color={statusMap[s]?.color}>{statusMap[s]?.text || s}</Tag>,
|
render: (s: string) => <Tag color={statusMap[s]?.color}>{statusMap[s]?.text || s}</Tag>,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: '退款状态',
|
|
||||||
dataIndex: 'refundStatus',
|
|
||||||
render: (s: string) =>
|
|
||||||
s ? <Tag color={refundStatusMap[s]?.color}>{refundStatusMap[s]?.text || s}</Tag> : '-',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
title: '退还金额',
|
title: '退还金额',
|
||||||
dataIndex: 'refundAmount',
|
dataIndex: 'refundAmount',
|
||||||
@@ -223,7 +217,7 @@ const DepositsPage: React.FC = () => {
|
|||||||
>
|
>
|
||||||
详情
|
详情
|
||||||
</PermissionButton>
|
</PermissionButton>
|
||||||
{record.status === 'paid' && !record.refundStatus && (
|
{record.status === 'paid' && (
|
||||||
<>
|
<>
|
||||||
<PermissionButton
|
<PermissionButton
|
||||||
permission="deposit:refund"
|
permission="deposit:refund"
|
||||||
@@ -320,7 +314,12 @@ const DepositsPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
scroll={{ x: 1200 }}
|
scroll={{ x: 1200 }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -410,14 +409,6 @@ const DepositsPage: React.FC = () => {
|
|||||||
{statusMap[detailModal.status]?.text || detailModal.status}
|
{statusMap[detailModal.status]?.text || detailModal.status}
|
||||||
</Tag>
|
</Tag>
|
||||||
</p>
|
</p>
|
||||||
{detailModal.refundStatus && (
|
|
||||||
<p>
|
|
||||||
<strong>退款状态:</strong>{' '}
|
|
||||||
<Tag color={refundStatusMap[detailModal.refundStatus]?.color}>
|
|
||||||
{refundStatusMap[detailModal.refundStatus]?.text || detailModal.refundStatus}
|
|
||||||
</Tag>
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
{detailModal.notes && <p><strong>备注:</strong> {detailModal.notes}</p>}
|
{detailModal.notes && <p><strong>备注:</strong> {detailModal.notes}</p>}
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,11 @@ import { message } from '../../ui/app-message';
|
|||||||
|
|
||||||
const { RangePicker } = DatePicker;
|
const { RangePicker } = DatePicker;
|
||||||
|
|
||||||
|
const isFormValidationError = (error: unknown) =>
|
||||||
|
typeof error === 'object'
|
||||||
|
&& error !== null
|
||||||
|
&& Array.isArray((error as { errorFields?: unknown }).errorFields);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const ExpensesPage: React.FC = () => {
|
const ExpensesPage: React.FC = () => {
|
||||||
@@ -157,16 +162,16 @@ const ExpensesPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleRoomExpense = async () => {
|
const handleRoomExpense = async () => {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
const values = await roomForm.validateFields();
|
|
||||||
const payload = {
|
|
||||||
roomId: values.roomId,
|
|
||||||
expenseType: values.expenseType,
|
|
||||||
amount: values.amount,
|
|
||||||
periodStart: values.period[0].format('YYYY-MM-DD'),
|
|
||||||
periodEnd: values.period[1].format('YYYY-MM-DD'),
|
|
||||||
description: values.description,
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
|
const values = await roomForm.validateFields();
|
||||||
|
const payload = {
|
||||||
|
roomId: values.roomId,
|
||||||
|
expenseType: values.expenseType,
|
||||||
|
amount: values.amount,
|
||||||
|
periodStart: values.period[0].format('YYYY-MM-DD'),
|
||||||
|
periodEnd: values.period[1].format('YYYY-MM-DD'),
|
||||||
|
description: values.description,
|
||||||
|
};
|
||||||
if (editingRoom) {
|
if (editingRoom) {
|
||||||
await api.put(`/expenses/room/${editingRoom.id}`, payload);
|
await api.put(`/expenses/room/${editingRoom.id}`, payload);
|
||||||
message.success('更新成功');
|
message.success('更新成功');
|
||||||
@@ -179,7 +184,9 @@ const ExpensesPage: React.FC = () => {
|
|||||||
roomForm.resetFields();
|
roomForm.resetFields();
|
||||||
fetchData();
|
fetchData();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.message || '操作失败');
|
if (!isFormValidationError(e)) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
}
|
}
|
||||||
@@ -187,16 +194,16 @@ const ExpensesPage: React.FC = () => {
|
|||||||
|
|
||||||
const handlePersonalExpense = async () => {
|
const handlePersonalExpense = async () => {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
const values = await personalForm.validateFields();
|
|
||||||
const payload = {
|
|
||||||
studentId: values.studentId,
|
|
||||||
roomId: values.roomId,
|
|
||||||
expenseType: values.expenseType,
|
|
||||||
amount: values.amount,
|
|
||||||
expenseDate: values.expenseDate.format('YYYY-MM-DD'),
|
|
||||||
description: values.description,
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
|
const values = await personalForm.validateFields();
|
||||||
|
const payload = {
|
||||||
|
studentId: values.studentId,
|
||||||
|
roomId: values.roomId,
|
||||||
|
expenseType: values.expenseType,
|
||||||
|
amount: values.amount,
|
||||||
|
expenseDate: values.expenseDate.format('YYYY-MM-DD'),
|
||||||
|
description: values.description,
|
||||||
|
};
|
||||||
if (editingPersonal) {
|
if (editingPersonal) {
|
||||||
await api.put(`/expenses/personal/${editingPersonal.id}`, payload);
|
await api.put(`/expenses/personal/${editingPersonal.id}`, payload);
|
||||||
message.success('更新成功');
|
message.success('更新成功');
|
||||||
@@ -209,7 +216,9 @@ const ExpensesPage: React.FC = () => {
|
|||||||
personalForm.resetFields();
|
personalForm.resetFields();
|
||||||
fetchData();
|
fetchData();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.message || '操作失败');
|
if (!isFormValidationError(e)) {
|
||||||
|
message.error(e?.message || '操作失败');
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
}
|
}
|
||||||
@@ -443,7 +452,12 @@ const ExpensesPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
scroll={{ x: 1200 }}
|
scroll={{ x: 1200 }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys: selectedRoomKeys,
|
selectedRowKeys: selectedRoomKeys,
|
||||||
@@ -566,7 +580,12 @@ const ExpensesPage: React.FC = () => {
|
|||||||
rowKey="id"
|
rowKey="id"
|
||||||
loading={loading}
|
loading={loading}
|
||||||
scroll={{ x: 1200 }}
|
scroll={{ x: 1200 }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys: selectedPersonalKeys,
|
selectedRowKeys: selectedPersonalKeys,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useState, useMemo, useCallback } from 'react';
|
import React, { useEffect, useState, useMemo, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
Card, Form, Input, Button, Space, Spin, Switch, Alert, Descriptions, Tag,
|
Card, Form, Input, Button, Space, Spin, Alert, Descriptions, Tag, Divider,
|
||||||
Tabs, Drawer, Tree, Select, TreeSelect, Modal, DatePicker, InputNumber,
|
Drawer, Tree, Select, TreeSelect, Modal, DatePicker, InputNumber,
|
||||||
Row, Col, List,
|
Row, Col, List,
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import {
|
import {
|
||||||
@@ -15,12 +15,15 @@ import api from '../../api';
|
|||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
import { usePermission } from '../../hooks/usePermission';
|
import { usePermission } from '../../hooks/usePermission';
|
||||||
import PermissionButton from '../../components/PermissionButton';
|
import PermissionButton from '../../components/PermissionButton';
|
||||||
|
import {
|
||||||
|
buildDingTalkConfigPayload,
|
||||||
|
isAppSecretRequired,
|
||||||
|
type DingTalkConfigFormValues,
|
||||||
|
} from './integration-config-form';
|
||||||
|
|
||||||
interface DingTalkConfig {
|
interface DingTalkConfig {
|
||||||
agentId: string;
|
agentId: string;
|
||||||
appSecret: string;
|
|
||||||
corpId: string;
|
corpId: string;
|
||||||
startEnable: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DingOrgTreeNodeExt {
|
interface DingOrgTreeNodeExt {
|
||||||
@@ -94,9 +97,9 @@ const IntegrationConfigPage: React.FC = () => {
|
|||||||
const [testing, setTesting] = useState(false);
|
const [testing, setTesting] = useState(false);
|
||||||
const [config, setConfig] = useState<DingTalkConfig | null>(null);
|
const [config, setConfig] = useState<DingTalkConfig | null>(null);
|
||||||
const [verified, setVerified] = useState<boolean | null>(null);
|
const [verified, setVerified] = useState<boolean | null>(null);
|
||||||
const [form] = Form.useForm<DingTalkConfig>();
|
const [form] = Form.useForm<DingTalkConfigFormValues>();
|
||||||
|
|
||||||
// ── Sync Users Tab ──
|
// ── Manual organization sync ──
|
||||||
const [syncRootDeptId, setSyncRootDeptId] = useState<number | undefined>(undefined);
|
const [syncRootDeptId, setSyncRootDeptId] = useState<number | undefined>(undefined);
|
||||||
const [orgTree, setOrgTree] = useState<DingOrgTreeNodeExt[]>([]);
|
const [orgTree, setOrgTree] = useState<DingOrgTreeNodeExt[]>([]);
|
||||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||||
@@ -137,9 +140,10 @@ const IntegrationConfigPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
const values = await form.validateFields();
|
const values = await form.validateFields();
|
||||||
|
const payload = buildDingTalkConfigPayload(values);
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
await api.post('/integration/config', { type: 'DINGTALK', config: values });
|
await api.post('/integration/config', { type: 'DINGTALK', config: payload });
|
||||||
message.success('配置已保存');
|
message.success('配置已保存');
|
||||||
await fetchConfig();
|
await fetchConfig();
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
@@ -152,11 +156,12 @@ const IntegrationConfigPage: React.FC = () => {
|
|||||||
|
|
||||||
const handleTest = async () => {
|
const handleTest = async () => {
|
||||||
const values = await form.validateFields();
|
const values = await form.validateFields();
|
||||||
|
const payload = buildDingTalkConfigPayload(values);
|
||||||
setTesting(true);
|
setTesting(true);
|
||||||
try {
|
try {
|
||||||
const res = await api.post<{ success: boolean; message: string }>('/integration/config/test', {
|
const res = await api.post<{ success: boolean; message: string }>('/integration/config/test', {
|
||||||
type: 'DINGTALK',
|
type: 'DINGTALK',
|
||||||
config: values,
|
config: payload,
|
||||||
});
|
});
|
||||||
setVerified(res.success);
|
setVerified(res.success);
|
||||||
message.success(res.message);
|
message.success(res.message);
|
||||||
@@ -345,12 +350,8 @@ const IntegrationConfigPage: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const syncTabItems = config && hasAllPermissions('sync:read', 'class:view', 'class:edit')
|
const syncPanel = config && hasAllPermissions('sync:read', 'class:view', 'class:edit')
|
||||||
? [
|
? (
|
||||||
{
|
|
||||||
key: 'sync-users',
|
|
||||||
label: '同步用户',
|
|
||||||
children: (
|
|
||||||
<div>
|
<div>
|
||||||
<Alert
|
<Alert
|
||||||
type="info"
|
type="info"
|
||||||
@@ -519,77 +520,100 @@ const IntegrationConfigPage: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
</Modal>
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
),
|
)
|
||||||
},
|
: null;
|
||||||
]
|
|
||||||
: [];
|
|
||||||
|
|
||||||
const tabItems = [
|
|
||||||
{
|
|
||||||
key: 'config',
|
|
||||||
label: '配置',
|
|
||||||
children: (
|
|
||||||
<Spin spinning={loading}>
|
|
||||||
{config && (
|
|
||||||
<Descriptions size="small" column={2} style={{ marginBottom: 24 }}>
|
|
||||||
<Descriptions.Item label="CorpId">{config.corpId || '-'}</Descriptions.Item>
|
|
||||||
<Descriptions.Item label="AppKey">{config.agentId || '-'}</Descriptions.Item>
|
|
||||||
<Descriptions.Item label="启用同步">
|
|
||||||
<Tag color={config.startEnable ? 'green' : 'default'}>
|
|
||||||
{config.startEnable ? '已启用' : '未启用'}
|
|
||||||
</Tag>
|
|
||||||
</Descriptions.Item>
|
|
||||||
</Descriptions>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Alert
|
|
||||||
type="info"
|
|
||||||
message="配置钉钉应用凭证后,可使用组织架构同步、考勤导入和排班同步功能。"
|
|
||||||
style={{ marginBottom: 24 }}
|
|
||||||
showIcon
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Form form={form} layout="vertical" style={{ maxWidth: 480 }}>
|
|
||||||
<Form.Item name="corpId" label="CorpId(企业ID)" rules={[{ required: true, message: '请输入 CorpId' }]}>
|
|
||||||
<Input placeholder="dingxxxxxxxx" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="agentId" label="AppKey(应用凭证)" rules={[{ required: true, message: '请输入 AppKey' }]}>
|
|
||||||
<Input placeholder="从钉钉开放平台获取" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item
|
|
||||||
name="appSecret"
|
|
||||||
label="AppSecret(应用密钥)"
|
|
||||||
rules={[{ required: true, message: '请输入 AppSecret' }]}
|
|
||||||
extra="保存后仅返回脱敏信息,重新编辑时需再次输入完整密钥"
|
|
||||||
>
|
|
||||||
<Input.Password placeholder="从钉钉开放平台获取" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="startEnable" label="启用同步" valuePropName="checked">
|
|
||||||
<Switch />
|
|
||||||
</Form.Item>
|
|
||||||
<Space>
|
|
||||||
<PermissionButton permission="integration:trigger" type="primary" icon={<SaveOutlined />} loading={saving} onClick={handleSave}>
|
|
||||||
保存配置
|
|
||||||
</PermissionButton>
|
|
||||||
<Button icon={<ApiOutlined />} loading={testing} onClick={handleTest}>
|
|
||||||
测试连接
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form>
|
|
||||||
</Spin>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
...syncTabItems,
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card title="钉钉集成配置" extra={
|
<Card
|
||||||
<Space>
|
title="钉钉集成配置"
|
||||||
{verified === true && <Tag icon={<CheckCircleOutlined />} color="success">已连接</Tag>}
|
extra={
|
||||||
{verified === false && <Tag icon={<CloseCircleOutlined />} color="error">未连接</Tag>}
|
<Space>
|
||||||
</Space>
|
{verified === true && (
|
||||||
}>
|
<Tag icon={<CheckCircleOutlined />} color="success">
|
||||||
<Tabs items={tabItems} />
|
已连接
|
||||||
|
</Tag>
|
||||||
|
)}
|
||||||
|
{verified === false && (
|
||||||
|
<Tag icon={<CloseCircleOutlined />} color="error">
|
||||||
|
未连接
|
||||||
|
</Tag>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Spin spinning={loading}>
|
||||||
|
{config && (
|
||||||
|
<Descriptions size="small" column={2} style={{ marginBottom: 24 }}>
|
||||||
|
<Descriptions.Item label="CorpId">{config.corpId || '-'}</Descriptions.Item>
|
||||||
|
<Descriptions.Item label="AppKey">{config.agentId || '-'}</Descriptions.Item>
|
||||||
|
<Descriptions.Item label="同步方式">手动触发</Descriptions.Item>
|
||||||
|
</Descriptions>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Alert
|
||||||
|
type="info"
|
||||||
|
message="配置钉钉应用凭证后,可在本页手动获取组织架构并导入用户。排班同步仍在排课管理中手动触发。"
|
||||||
|
style={{ marginBottom: 24 }}
|
||||||
|
showIcon
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Form form={form} layout="vertical" style={{ maxWidth: 520 }}>
|
||||||
|
<Form.Item
|
||||||
|
name="corpId"
|
||||||
|
label="CorpId(企业ID)"
|
||||||
|
rules={[{ required: true, message: '请输入 CorpId' }]}
|
||||||
|
>
|
||||||
|
<Input placeholder="dingxxxxxxxx" />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
name="agentId"
|
||||||
|
label="AppKey(应用凭证)"
|
||||||
|
rules={[{ required: true, message: '请输入 AppKey' }]}
|
||||||
|
>
|
||||||
|
<Input placeholder="从钉钉开放平台获取" />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
name="appSecret"
|
||||||
|
label="AppSecret(应用密钥)"
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: isAppSecretRequired(!!config),
|
||||||
|
message: '首次配置请输入 AppSecret',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
extra={
|
||||||
|
config
|
||||||
|
? '已保存密钥;留空保持原值,输入新值将替换原密钥'
|
||||||
|
: '首次配置需要填写完整 AppSecret'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Input.Password
|
||||||
|
placeholder={config ? '留空保持已保存的密钥' : '从钉钉开放平台获取'}
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
<Space>
|
||||||
|
<PermissionButton
|
||||||
|
permission="integration:trigger"
|
||||||
|
type="primary"
|
||||||
|
icon={<SaveOutlined />}
|
||||||
|
loading={saving}
|
||||||
|
onClick={handleSave}
|
||||||
|
>
|
||||||
|
保存配置
|
||||||
|
</PermissionButton>
|
||||||
|
<Button icon={<ApiOutlined />} loading={testing} onClick={handleTest}>
|
||||||
|
测试连接
|
||||||
|
</Button>
|
||||||
|
</Space>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
{syncPanel && (
|
||||||
|
<>
|
||||||
|
<Divider titlePlacement="start">组织用户导入</Divider>
|
||||||
|
{syncPanel}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Spin>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
buildDingTalkConfigPayload,
|
||||||
|
isAppSecretRequired,
|
||||||
|
} from './integration-config-form';
|
||||||
|
|
||||||
|
describe('DingTalk integration config form', () => {
|
||||||
|
it('requires AppSecret only for the first configuration', () => {
|
||||||
|
expect(isAppSecretRequired(false)).toBe(true);
|
||||||
|
expect(isAppSecretRequired(true)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('builds a manual-sync config without the retired startEnable flag', () => {
|
||||||
|
expect(
|
||||||
|
buildDingTalkConfigPayload({
|
||||||
|
corpId: 'ding-corp',
|
||||||
|
agentId: 'app-key',
|
||||||
|
appSecret: '',
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
corpId: 'ding-corp',
|
||||||
|
agentId: 'app-key',
|
||||||
|
appSecret: undefined,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
export interface DingTalkConfigFormValues {
|
||||||
|
agentId: string;
|
||||||
|
appSecret?: string;
|
||||||
|
corpId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isAppSecretRequired = (hasSavedConfig: boolean) => !hasSavedConfig;
|
||||||
|
|
||||||
|
export const buildDingTalkConfigPayload = (values: DingTalkConfigFormValues) => ({
|
||||||
|
corpId: values.corpId.trim(),
|
||||||
|
agentId: values.agentId.trim(),
|
||||||
|
appSecret: values.appSecret?.trim() || undefined,
|
||||||
|
});
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import api from '../../api';
|
import api from '../../api';
|
||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
|
import { formatNotificationText } from '../../utils/notification-display';
|
||||||
|
|
||||||
const { Sider, Content } = Layout;
|
const { Sider, Content } = Layout;
|
||||||
|
|
||||||
@@ -169,7 +170,7 @@ const NotificationsPage: React.FC = () => {
|
|||||||
strong={!item.isRead}
|
strong={!item.isRead}
|
||||||
style={{ fontSize: 15 }}
|
style={{ fontSize: 15 }}
|
||||||
>
|
>
|
||||||
{item.title}
|
{formatNotificationText(item.title)}
|
||||||
</Typography.Text>
|
</Typography.Text>
|
||||||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
||||||
{timeAgo(item.createdAt)}
|
{timeAgo(item.createdAt)}
|
||||||
@@ -183,7 +184,7 @@ const NotificationsPage: React.FC = () => {
|
|||||||
ellipsis={{ rows: 1 }}
|
ellipsis={{ rows: 1 }}
|
||||||
style={{ marginBottom: 0 }}
|
style={{ marginBottom: 0 }}
|
||||||
>
|
>
|
||||||
{item.content}
|
{formatNotificationText(item.content)}
|
||||||
</Typography.Paragraph>
|
</Typography.Paragraph>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import { downloadBlob } from '../../utils/download';
|
|||||||
import { maskPhone, maskIdNumber } from '../../utils/sensitive';
|
import { maskPhone, maskIdNumber } from '../../utils/sensitive';
|
||||||
import PermissionButton from '../../components/PermissionButton';
|
import PermissionButton from '../../components/PermissionButton';
|
||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
|
import { buildTransferPayload } from './occupancy-form';
|
||||||
|
|
||||||
const { RangePicker } = DatePicker;
|
const { RangePicker } = DatePicker;
|
||||||
|
|
||||||
@@ -59,6 +60,8 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
const [batchCheckOutForm] = Form.useForm();
|
const [batchCheckOutForm] = Form.useForm();
|
||||||
const [availableBeds, setAvailableBeds] = useState<any[]>([]);
|
const [availableBeds, setAvailableBeds] = useState<any[]>([]);
|
||||||
const [availableLockers, setAvailableLockers] = useState<any[]>([]);
|
const [availableLockers, setAvailableLockers] = useState<any[]>([]);
|
||||||
|
const [transferAvailableBeds, setTransferAvailableBeds] = useState<any[]>([]);
|
||||||
|
const [transferAvailableLockers, setTransferAvailableLockers] = useState<any[]>([]);
|
||||||
|
|
||||||
const fetchData = useCallback(async () => {
|
const fetchData = useCallback(async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -110,6 +113,30 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
} catch (e) { console.error(e); }
|
} catch (e) { console.error(e); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleTransferRoomChange = async (roomId: number) => {
|
||||||
|
transferForm.setFieldValue('newBedId', undefined);
|
||||||
|
transferForm.setFieldValue('newLockerId', undefined);
|
||||||
|
if (!roomId) {
|
||||||
|
setTransferAvailableBeds([]);
|
||||||
|
setTransferAvailableLockers([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const [beds, lockers] = await Promise.all([
|
||||||
|
api.get<any[]>(`/rooms/${roomId}/beds/available`),
|
||||||
|
api.get<any[]>(`/rooms/${roomId}/lockers/available`),
|
||||||
|
]);
|
||||||
|
setTransferAvailableBeds(beds);
|
||||||
|
setTransferAvailableLockers(lockers);
|
||||||
|
if (beds.length === 1) transferForm.setFieldValue('newBedId', beds[0].id);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setTransferAvailableBeds([]);
|
||||||
|
setTransferAvailableLockers([]);
|
||||||
|
message.error('目标宿舍床位和柜子加载失败');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const filteredData = useMemo(() => {
|
const filteredData = useMemo(() => {
|
||||||
if (!searchText) return data;
|
if (!searchText) return data;
|
||||||
const keyword = searchText.toLowerCase();
|
const keyword = searchText.toLowerCase();
|
||||||
@@ -170,13 +197,10 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
const values = await transferForm.validateFields();
|
const values = await transferForm.validateFields();
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
await api.put(`/occupancies/${transferModal.id}/transfer`, {
|
await api.put(
|
||||||
newRoomId: values.newRoomId,
|
`/occupancies/${transferModal.id}/transfer`,
|
||||||
transferDate: values.transferDate.format('YYYY-MM-DD'),
|
buildTransferPayload(values),
|
||||||
oldBillingEndDate: values.oldBillingEndDate?.format('YYYY-MM-DD'),
|
);
|
||||||
newBillingStartDate: values.newBillingStartDate?.format('YYYY-MM-DD'),
|
|
||||||
reason: values.reason,
|
|
||||||
});
|
|
||||||
message.success('换房成功');
|
message.success('换房成功');
|
||||||
setTransferModal(null);
|
setTransferModal(null);
|
||||||
transferForm.resetFields();
|
transferForm.resetFields();
|
||||||
@@ -261,6 +285,9 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
size="small"
|
size="small"
|
||||||
icon={<SwapOutlined />}
|
icon={<SwapOutlined />}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setTransferAvailableBeds([]);
|
||||||
|
setTransferAvailableLockers([]);
|
||||||
|
transferForm.resetFields();
|
||||||
setTransferModal(record);
|
setTransferModal(record);
|
||||||
transferForm.setFieldsValue({ transferDate: dayjs() });
|
transferForm.setFieldsValue({ transferDate: dayjs() });
|
||||||
}}
|
}}
|
||||||
@@ -495,7 +522,12 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
scroll={{ x: 1300 }}
|
scroll={{ x: 1300 }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 条` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 条`,
|
||||||
|
}}
|
||||||
rowSelection={rowSelection}
|
rowSelection={rowSelection}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -708,7 +740,12 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
title={`换房 - ${transferModal?.student?.name}`}
|
title={`换房 - ${transferModal?.student?.name}`}
|
||||||
open={!!transferModal}
|
open={!!transferModal}
|
||||||
onOk={handleTransfer}
|
onOk={handleTransfer}
|
||||||
onCancel={() => setTransferModal(null)}
|
onCancel={() => {
|
||||||
|
setTransferModal(null);
|
||||||
|
transferForm.resetFields();
|
||||||
|
setTransferAvailableBeds([]);
|
||||||
|
setTransferAvailableLockers([]);
|
||||||
|
}}
|
||||||
okText="确认换房"
|
okText="确认换房"
|
||||||
confirmLoading={saving}
|
confirmLoading={saving}
|
||||||
width={500}
|
width={500}
|
||||||
@@ -719,6 +756,7 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
showSearch
|
showSearch
|
||||||
optionFilterProp="label"
|
optionFilterProp="label"
|
||||||
placeholder="选择目标宿舍"
|
placeholder="选择目标宿舍"
|
||||||
|
onChange={handleTransferRoomChange}
|
||||||
options={rooms
|
options={rooms
|
||||||
.filter((r: any) => r.id !== transferModal?.roomId)
|
.filter((r: any) => r.id !== transferModal?.roomId)
|
||||||
.map((r: any) => ({
|
.map((r: any) => ({
|
||||||
@@ -728,6 +766,38 @@ const OccupanciesPage: React.FC = () => {
|
|||||||
}))}
|
}))}
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
name="newBedId"
|
||||||
|
label="目标床位"
|
||||||
|
rules={[{ required: true, message: '请选择目标床位' }]}
|
||||||
|
>
|
||||||
|
<Select
|
||||||
|
placeholder="请先选择目标宿舍"
|
||||||
|
disabled={transferAvailableBeds.length === 0}
|
||||||
|
options={transferAvailableBeds.map((bed) => ({
|
||||||
|
value: bed.id,
|
||||||
|
label: bed.bedNumber,
|
||||||
|
}))}
|
||||||
|
notFoundContent="目标宿舍暂无可用床位"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
{transferAvailableBeds.length > 0 && (
|
||||||
|
<div style={{ marginTop: -16, marginBottom: 16, color: '#888', fontSize: 12 }}>
|
||||||
|
空闲 {transferAvailableBeds.length} 张床位
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Form.Item name="newLockerId" label="目标柜子(可选)">
|
||||||
|
<Select
|
||||||
|
allowClear
|
||||||
|
placeholder="可选分配目标宿舍柜子"
|
||||||
|
disabled={transferAvailableLockers.length === 0}
|
||||||
|
options={transferAvailableLockers.map((locker) => ({
|
||||||
|
value: locker.id,
|
||||||
|
label: locker.lockerNumber,
|
||||||
|
}))}
|
||||||
|
notFoundContent="目标宿舍暂无可用柜子"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
<Form.Item name="transferDate" label="换房日期" rules={[{ required: true }]}>
|
<Form.Item name="transferDate" label="换房日期" rules={[{ required: true }]}>
|
||||||
<DatePicker style={{ width: '100%' }} placeholder="选择换房日期" format="YYYY-MM-DD" />
|
<DatePicker style={{ width: '100%' }} placeholder="选择换房日期" format="YYYY-MM-DD" />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { buildTransferPayload } from './occupancy-form';
|
||||||
|
|
||||||
|
describe('occupancy transfer form', () => {
|
||||||
|
it('submits the target room resources with the transfer dates', () => {
|
||||||
|
expect(
|
||||||
|
buildTransferPayload({
|
||||||
|
newRoomId: 5,
|
||||||
|
newBedId: 12,
|
||||||
|
newLockerId: 18,
|
||||||
|
transferDate: dayjs('2026-07-13'),
|
||||||
|
oldBillingEndDate: dayjs('2026-07-13'),
|
||||||
|
newBillingStartDate: dayjs('2026-07-14'),
|
||||||
|
reason: '调整宿舍',
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
newRoomId: 5,
|
||||||
|
newBedId: 12,
|
||||||
|
newLockerId: 18,
|
||||||
|
transferDate: '2026-07-13',
|
||||||
|
oldBillingEndDate: '2026-07-13',
|
||||||
|
newBillingStartDate: '2026-07-14',
|
||||||
|
reason: '调整宿舍',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
21
apps/admin/src/pages/Occupancies/occupancy-form.ts
Normal file
21
apps/admin/src/pages/Occupancies/occupancy-form.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import type { Dayjs } from 'dayjs';
|
||||||
|
|
||||||
|
export interface TransferFormValues {
|
||||||
|
newRoomId: number;
|
||||||
|
newBedId: number;
|
||||||
|
newLockerId?: number;
|
||||||
|
transferDate: Dayjs;
|
||||||
|
oldBillingEndDate?: Dayjs;
|
||||||
|
newBillingStartDate?: Dayjs;
|
||||||
|
reason?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const buildTransferPayload = (values: TransferFormValues) => ({
|
||||||
|
newRoomId: values.newRoomId,
|
||||||
|
newBedId: values.newBedId,
|
||||||
|
newLockerId: values.newLockerId || undefined,
|
||||||
|
transferDate: values.transferDate.format('YYYY-MM-DD'),
|
||||||
|
oldBillingEndDate: values.oldBillingEndDate?.format('YYYY-MM-DD'),
|
||||||
|
newBillingStartDate: values.newBillingStartDate?.format('YYYY-MM-DD'),
|
||||||
|
reason: values.reason,
|
||||||
|
});
|
||||||
@@ -26,13 +26,14 @@ const OperationLogsPage: React.FC = () => {
|
|||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
|
const [pageSize, setPageSize] = useState(20);
|
||||||
const [filterModule, setFilterModule] = useState<string | undefined>();
|
const [filterModule, setFilterModule] = useState<string | undefined>();
|
||||||
const [dateRange, setDateRange] = useState<[string, string] | null>(null);
|
const [dateRange, setDateRange] = useState<[string, string] | null>(null);
|
||||||
|
|
||||||
const fetchData = useCallback(async () => {
|
const fetchData = useCallback(async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const params: any = { page, pageSize: 20 };
|
const params: any = { page, pageSize };
|
||||||
if (filterModule) params.module = filterModule;
|
if (filterModule) params.module = filterModule;
|
||||||
if (dateRange) {
|
if (dateRange) {
|
||||||
params.startDate = dateRange[0];
|
params.startDate = dateRange[0];
|
||||||
@@ -46,7 +47,7 @@ const OperationLogsPage: React.FC = () => {
|
|||||||
message.error(err?.message || '加载失败,请稍后重试');
|
message.error(err?.message || '加载失败,请稍后重试');
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, [page, filterModule, dateRange]);
|
}, [page, pageSize, filterModule, dateRange]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
@@ -159,8 +160,13 @@ const OperationLogsPage: React.FC = () => {
|
|||||||
pagination={{
|
pagination={{
|
||||||
current: page,
|
current: page,
|
||||||
total,
|
total,
|
||||||
pageSize: 20,
|
pageSize,
|
||||||
onChange: setPage,
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
onChange: (nextPage, nextPageSize) => {
|
||||||
|
setPage(nextPage);
|
||||||
|
setPageSize(nextPageSize);
|
||||||
|
},
|
||||||
showTotal: (t) => `共 ${t} 条`,
|
showTotal: (t) => `共 ${t} 条`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -235,7 +235,12 @@ const OrganizationsPage: React.FC = () => {
|
|||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无机构" /> }}
|
locale={{ emptyText: <Empty description="暂无机构" /> }}
|
||||||
scroll={{ x: 1100 }}
|
scroll={{ x: 1100 }}
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 个机构` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 个机构`,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
title={editing ? `编辑机构 · ${editing.name}` : '添加外部机构'}
|
title={editing ? `编辑机构 · ${editing.name}` : '添加外部机构'}
|
||||||
|
|||||||
@@ -156,17 +156,23 @@ const RoomsPage: React.FC = () => {
|
|||||||
if (filterStatus) result = result.filter((r: Record<string, unknown>) => r.status === filterStatus);
|
if (filterStatus) result = result.filter((r: Record<string, unknown>) => r.status === filterStatus);
|
||||||
return result;
|
return result;
|
||||||
}, [data, searchText, filterBuilding, filterStatus]);
|
}, [data, searchText, filterBuilding, filterStatus]);
|
||||||
|
const remainingBedSlots = useMemo(() => {
|
||||||
|
const capacity = Number(drawerRoom?.capacity) || 0;
|
||||||
|
return Math.max(capacity - beds.length, 0);
|
||||||
|
}, [drawerRoom?.capacity, beds.length]);
|
||||||
|
const defaultBatchBedCount = Math.min(4, Math.max(remainingBedSlots, 1));
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
const values = await form.validateFields();
|
const values = await form.validateFields();
|
||||||
|
const payload = values;
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
if (editing) {
|
if (editing) {
|
||||||
await api.put(`/rooms/${editing.id}`, values);
|
await api.put(`/rooms/${editing.id}`, payload);
|
||||||
message.success('更新成功');
|
message.success('更新成功');
|
||||||
} else {
|
} else {
|
||||||
await api.post('/rooms', values);
|
await api.post('/rooms', payload);
|
||||||
message.success('创建成功');
|
message.success(`创建成功,已自动生成 ${values.capacity} 张床位`);
|
||||||
}
|
}
|
||||||
setModalOpen(false);
|
setModalOpen(false);
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
@@ -337,12 +343,6 @@ const RoomsPage: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: '性别',
|
|
||||||
dataIndex: 'gender',
|
|
||||||
width: 80,
|
|
||||||
render: (v: any) => (v ? <Tag color={v === '男' ? 'blue' : 'pink'}>{v}</Tag> : '-'),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
title: '状态',
|
title: '状态',
|
||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
@@ -519,7 +519,12 @@ const RoomsPage: React.FC = () => {
|
|||||||
scroll={{ x: 1200 }}
|
scroll={{ x: 1200 }}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 间` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 间`,
|
||||||
|
}}
|
||||||
rowClassName={(record) => (record.status === 'archived' ? 'archived-row' : '')}
|
rowClassName={(record) => (record.status === 'archived' ? 'archived-row' : '')}
|
||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys,
|
selectedRowKeys,
|
||||||
@@ -634,24 +639,26 @@ const RoomsPage: React.FC = () => {
|
|||||||
type="primary"
|
type="primary"
|
||||||
size="small"
|
size="small"
|
||||||
icon={<PlusOutlined />}
|
icon={<PlusOutlined />}
|
||||||
disabled={drawerRoom?.status === 'archived'}
|
disabled={drawerRoom?.status === 'archived' || remainingBedSlots === 0}
|
||||||
onClick={() => { setBedEditing(null); bedForm.resetFields(); setBedModalOpen(true); }}
|
onClick={() => { setBedEditing(null); bedForm.resetFields(); setBedModalOpen(true); }}
|
||||||
>
|
>
|
||||||
添加床位
|
添加床位
|
||||||
</Button>
|
</Button>
|
||||||
<Popconfirm
|
<Popconfirm
|
||||||
title="批量生成床位"
|
title={remainingBedSlots > 0 ? '批量生成床位' : '床位已达到额定人数'}
|
||||||
description={
|
description={
|
||||||
<InputNumber min={1} max={20} defaultValue={4} id="batch-bed-count" style={{ width: 80 }} />
|
remainingBedSlots > 0
|
||||||
|
? <InputNumber min={1} max={remainingBedSlots} defaultValue={defaultBatchBedCount} id="batch-bed-count" style={{ width: 80 }} />
|
||||||
|
: '如需增加床位,请先调整宿舍额定人数'
|
||||||
}
|
}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
const input = document.getElementById('batch-bed-count') as HTMLInputElement;
|
const input = document.getElementById('batch-bed-count') as HTMLInputElement;
|
||||||
handleBatchBeds(input ? parseInt(input.value) || 4 : 4);
|
handleBatchBeds(input ? parseInt(input.value) || defaultBatchBedCount : defaultBatchBedCount);
|
||||||
}}
|
}}
|
||||||
okText="生成"
|
okText="生成"
|
||||||
disabled={drawerRoom?.status === 'archived'}
|
disabled={drawerRoom?.status === 'archived' || remainingBedSlots === 0}
|
||||||
>
|
>
|
||||||
<Button size="small" disabled={drawerRoom?.status === 'archived'}>批量生成</Button>
|
<Button size="small" disabled={drawerRoom?.status === 'archived' || remainingBedSlots === 0}>批量生成</Button>
|
||||||
</Popconfirm>
|
</Popconfirm>
|
||||||
</div>
|
</div>
|
||||||
<Table
|
<Table
|
||||||
|
|||||||
@@ -947,6 +947,19 @@ const SchedulesPage: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
name="notes"
|
||||||
|
label="备注"
|
||||||
|
rules={[{ max: 500, message: '备注不能超过500字' }]}
|
||||||
|
>
|
||||||
|
<Input.TextArea
|
||||||
|
rows={3}
|
||||||
|
maxLength={500}
|
||||||
|
showCount
|
||||||
|
placeholder="可填写排课说明、设备需求或临时调整原因"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item
|
<Form.Item
|
||||||
name="timeRange"
|
name="timeRange"
|
||||||
label="上课时段"
|
label="上课时段"
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ describe('schedule edit form mapping', () => {
|
|||||||
endTime: '18:00',
|
endTime: '18:00',
|
||||||
startDate: '2026-07-01',
|
startDate: '2026-07-01',
|
||||||
endDate: '2026-07-31',
|
endDate: '2026-07-31',
|
||||||
|
notes: '需要投影设备',
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(values.classroomId).toBe(1);
|
expect(values.classroomId).toBe(1);
|
||||||
expect(values.weekDay).toBe(5);
|
expect(values.weekDay).toBe(5);
|
||||||
|
expect(values.notes).toBe('需要投影设备');
|
||||||
expect(values.timeRange.map((item) => item.format('HH:mm'))).toEqual(['14:00', '18:00']);
|
expect(values.timeRange.map((item) => item.format('HH:mm'))).toEqual(['14:00', '18:00']);
|
||||||
expect(values.dateRange.map((item) => item.format('YYYY-MM-DD'))).toEqual([
|
expect(values.dateRange.map((item) => item.format('YYYY-MM-DD'))).toEqual([
|
||||||
'2026-07-01',
|
'2026-07-01',
|
||||||
@@ -36,6 +38,7 @@ describe('schedule edit form mapping', () => {
|
|||||||
teacherId: 4,
|
teacherId: 4,
|
||||||
timeRange: [dayjs('2026-01-01 13:30'), dayjs('2026-01-01 17:20')],
|
timeRange: [dayjs('2026-01-01 13:30'), dayjs('2026-01-01 17:20')],
|
||||||
dateRange: [dayjs('2026-08-01'), dayjs('2026-08-31')],
|
dateRange: [dayjs('2026-08-01'), dayjs('2026-08-31')],
|
||||||
|
notes: ' 临时调整教室 ',
|
||||||
}),
|
}),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
classId: 1,
|
classId: 1,
|
||||||
@@ -47,6 +50,24 @@ describe('schedule edit form mapping', () => {
|
|||||||
endTime: '17:20',
|
endTime: '17:20',
|
||||||
startDate: '2026-08-01',
|
startDate: '2026-08-01',
|
||||||
endDate: '2026-08-31',
|
endDate: '2026-08-31',
|
||||||
|
notes: '临时调整教室',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('schedule notes normalization', () => {
|
||||||
|
it('omits whitespace-only notes from the payload', () => {
|
||||||
|
expect(
|
||||||
|
buildSchedulePayload({
|
||||||
|
classId: 1,
|
||||||
|
classroomId: 2,
|
||||||
|
weekDay: 6,
|
||||||
|
subject: '作文',
|
||||||
|
timeRange: [dayjs('2026-01-01 13:30'), dayjs('2026-01-01 17:20')],
|
||||||
|
dateRange: [dayjs('2026-08-01'), dayjs('2026-08-31')],
|
||||||
|
notes: ' ',
|
||||||
|
}).notes,
|
||||||
|
).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export interface ScheduleFormValues {
|
|||||||
weekDay: number;
|
weekDay: number;
|
||||||
subject: string;
|
subject: string;
|
||||||
teacherId?: number;
|
teacherId?: number;
|
||||||
|
notes?: string;
|
||||||
timeRange: [Dayjs, Dayjs];
|
timeRange: [Dayjs, Dayjs];
|
||||||
dateRange: [Dayjs, Dayjs];
|
dateRange: [Dayjs, Dayjs];
|
||||||
}
|
}
|
||||||
@@ -17,6 +18,7 @@ export interface EditableSchedule {
|
|||||||
weekDay: number;
|
weekDay: number;
|
||||||
subject: string;
|
subject: string;
|
||||||
teacherId: number | null;
|
teacherId: number | null;
|
||||||
|
notes?: string | null;
|
||||||
startTime: string;
|
startTime: string;
|
||||||
endTime: string;
|
endTime: string;
|
||||||
startDate: string;
|
startDate: string;
|
||||||
@@ -29,6 +31,7 @@ export const scheduleToFormValues = (schedule: EditableSchedule): ScheduleFormVa
|
|||||||
weekDay: schedule.weekDay,
|
weekDay: schedule.weekDay,
|
||||||
subject: schedule.subject,
|
subject: schedule.subject,
|
||||||
teacherId: schedule.teacherId ?? undefined,
|
teacherId: schedule.teacherId ?? undefined,
|
||||||
|
notes: schedule.notes ?? undefined,
|
||||||
timeRange: [dayjs(`2000-01-01 ${schedule.startTime}`), dayjs(`2000-01-01 ${schedule.endTime}`)],
|
timeRange: [dayjs(`2000-01-01 ${schedule.startTime}`), dayjs(`2000-01-01 ${schedule.endTime}`)],
|
||||||
dateRange: [dayjs(schedule.startDate), dayjs(schedule.endDate)],
|
dateRange: [dayjs(schedule.startDate), dayjs(schedule.endDate)],
|
||||||
});
|
});
|
||||||
@@ -39,6 +42,7 @@ export const buildSchedulePayload = (values: ScheduleFormValues) => ({
|
|||||||
weekDay: values.weekDay,
|
weekDay: values.weekDay,
|
||||||
subject: values.subject,
|
subject: values.subject,
|
||||||
teacherId: values.teacherId,
|
teacherId: values.teacherId,
|
||||||
|
notes: values.notes?.trim() || undefined,
|
||||||
startTime: values.timeRange[0].format('HH:mm'),
|
startTime: values.timeRange[0].format('HH:mm'),
|
||||||
endTime: values.timeRange[1].format('HH:mm'),
|
endTime: values.timeRange[1].format('HH:mm'),
|
||||||
startDate: values.dateRange[0].format('YYYY-MM-DD'),
|
startDate: values.dateRange[0].format('YYYY-MM-DD'),
|
||||||
|
|||||||
@@ -322,7 +322,28 @@ const StudentsPage: React.FC = () => {
|
|||||||
},
|
},
|
||||||
{ title: '民族', dataIndex: 'ethnicity', width: 90 },
|
{ title: '民族', dataIndex: 'ethnicity', width: 90 },
|
||||||
{ title: '紧急联系人', dataIndex: 'emergencyContact', width: 100 },
|
{ title: '紧急联系人', dataIndex: 'emergencyContact', width: 100 },
|
||||||
{ title: '紧急联系人电话', dataIndex: 'emergencyPhone', width: 130 },
|
{
|
||||||
|
title: '紧急联系人电话',
|
||||||
|
dataIndex: 'emergencyPhone',
|
||||||
|
width: 150,
|
||||||
|
render: (v: string, record: any) => {
|
||||||
|
if (!v) return '-';
|
||||||
|
return (
|
||||||
|
<span>
|
||||||
|
<span style={{ marginRight: 4 }}>{maskPhone(v)}</span>
|
||||||
|
<Button
|
||||||
|
type="link"
|
||||||
|
size="small"
|
||||||
|
style={{ padding: '8px 4px' }}
|
||||||
|
onClick={() => handleViewSensitive(record.id, '紧急联系人电话', v)}
|
||||||
|
title="点击查看完整号码"
|
||||||
|
>
|
||||||
|
<EyeOutlined style={{ fontSize: 12, color: '#999' }} />
|
||||||
|
</Button>
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: '所属机构',
|
title: '所属机构',
|
||||||
dataIndex: 'organization',
|
dataIndex: 'organization',
|
||||||
@@ -556,7 +577,12 @@ const StudentsPage: React.FC = () => {
|
|||||||
loading={loading}
|
loading={loading}
|
||||||
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
locale={{ emptyText: <Empty description="暂无数据" /> }}
|
||||||
scroll={{ x: 1410 }}
|
scroll={{ x: 1410 }}
|
||||||
pagination={{ pageSize: 15, showTotal: (total) => `共 ${total} 人` }}
|
pagination={{
|
||||||
|
defaultPageSize: 15,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [15, 30, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 人`,
|
||||||
|
}}
|
||||||
rowClassName={(record: any) => (record.status === 'archived' ? 'archived-row' : '')}
|
rowClassName={(record: any) => (record.status === 'archived' ? 'archived-row' : '')}
|
||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys,
|
selectedRowKeys,
|
||||||
|
|||||||
@@ -146,7 +146,12 @@ const TeacherWorkspacePage: React.FC = () => {
|
|||||||
columns={classColumns}
|
columns={classColumns}
|
||||||
dataSource={data.assignedClasses}
|
dataSource={data.assignedClasses}
|
||||||
rowKey="classId"
|
rowKey="classId"
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 个班级` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 个班级`,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Empty description="暂无分配的班级" />
|
<Empty description="暂无分配的班级" />
|
||||||
@@ -160,7 +165,12 @@ const TeacherWorkspacePage: React.FC = () => {
|
|||||||
columns={scheduleColumns}
|
columns={scheduleColumns}
|
||||||
dataSource={data.todaySchedules}
|
dataSource={data.todaySchedules}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 节` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 节`,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Empty description="今日无排课" />
|
<Empty description="今日无排课" />
|
||||||
@@ -174,7 +184,12 @@ const TeacherWorkspacePage: React.FC = () => {
|
|||||||
columns={studentColumns}
|
columns={studentColumns}
|
||||||
dataSource={data.myStudents}
|
dataSource={data.myStudents}
|
||||||
rowKey="studentId"
|
rowKey="studentId"
|
||||||
pagination={{ pageSize: 20, showTotal: (total) => `共 ${total} 人` }}
|
pagination={{
|
||||||
|
defaultPageSize: 20,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
showTotal: (total) => `共 ${total} 人`,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Empty description="暂无学生" />
|
<Empty description="暂无学生" />
|
||||||
|
|||||||
@@ -46,13 +46,14 @@ const ROLE_TYPE_LABELS: Record<string, string> = {
|
|||||||
academic_teacher: '教务老师',
|
academic_teacher: '教务老师',
|
||||||
};
|
};
|
||||||
|
|
||||||
const PAGE_SIZE = 20;
|
const DEFAULT_PAGE_SIZE = 20;
|
||||||
|
|
||||||
const TeachersPage: React.FC = () => {
|
const TeachersPage: React.FC = () => {
|
||||||
const [data, setData] = useState<TeacherRow[]>([]);
|
const [data, setData] = useState<TeacherRow[]>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
|
const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE);
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
const [profileModal, setProfileModal] = useState<TeacherRow | null>(null);
|
const [profileModal, setProfileModal] = useState<TeacherRow | null>(null);
|
||||||
const [form] = Form.useForm<ProfileFormValues>();
|
const [form] = Form.useForm<ProfileFormValues>();
|
||||||
@@ -62,7 +63,7 @@ const TeachersPage: React.FC = () => {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await api.get<TeacherListResponse>('/rbac/teachers', {
|
const res = await api.get<TeacherListResponse>('/rbac/teachers', {
|
||||||
params: { search: search || undefined, page, pageSize: PAGE_SIZE },
|
params: { search: search || undefined, page, pageSize },
|
||||||
});
|
});
|
||||||
setData(res.list);
|
setData(res.list);
|
||||||
setTotal(res.total);
|
setTotal(res.total);
|
||||||
@@ -70,7 +71,7 @@ const TeachersPage: React.FC = () => {
|
|||||||
// silent
|
// silent
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, [page, search]);
|
}, [page, pageSize, search]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
@@ -199,9 +200,14 @@ const TeachersPage: React.FC = () => {
|
|||||||
scroll={{ x: 1300 }}
|
scroll={{ x: 1300 }}
|
||||||
pagination={{
|
pagination={{
|
||||||
current: page,
|
current: page,
|
||||||
pageSize: PAGE_SIZE,
|
pageSize,
|
||||||
total,
|
total,
|
||||||
onChange: setPage,
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: [20, 50, 100],
|
||||||
|
onChange: (nextPage, nextPageSize) => {
|
||||||
|
setPage(nextPage);
|
||||||
|
setPageSize(nextPageSize);
|
||||||
|
},
|
||||||
showTotal: (t) => `共 ${t} 人`,
|
showTotal: (t) => `共 ${t} 人`,
|
||||||
}}
|
}}
|
||||||
expandable={{
|
expandable={{
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ import dayjs from 'dayjs';
|
|||||||
import api from '../../api';
|
import api from '../../api';
|
||||||
import PermissionButton from '../../components/PermissionButton';
|
import PermissionButton from '../../components/PermissionButton';
|
||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
|
import {
|
||||||
|
userProfileResponseToFormValues,
|
||||||
|
type UserProfileResponse,
|
||||||
|
} from './user-profile-form';
|
||||||
|
|
||||||
const UsersPage: React.FC = () => {
|
const UsersPage: React.FC = () => {
|
||||||
const [data, setData] = useState<any[]>([]);
|
const [data, setData] = useState<any[]>([]);
|
||||||
@@ -36,8 +40,8 @@ const UsersPage: React.FC = () => {
|
|||||||
const handleOpenProfile = async (record: any) => {
|
const handleOpenProfile = async (record: any) => {
|
||||||
setProfileUser(record);
|
setProfileUser(record);
|
||||||
try {
|
try {
|
||||||
const res: any = await api.get(`/rbac/users/${record.id}/profile`);
|
const res = await api.get<UserProfileResponse>(`/rbac/users/${record.id}/profile`);
|
||||||
profileForm.setFieldsValue(res);
|
profileForm.setFieldsValue(userProfileResponseToFormValues(res));
|
||||||
} catch {
|
} catch {
|
||||||
profileForm.setFieldsValue({});
|
profileForm.setFieldsValue({});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { userProfileResponseToFormValues } from './user-profile-form';
|
||||||
|
|
||||||
|
describe('user profile form mapping', () => {
|
||||||
|
it('unwraps the nested profile returned by the user profile endpoint', () => {
|
||||||
|
expect(
|
||||||
|
userProfileResponseToFormValues({
|
||||||
|
id: 9,
|
||||||
|
username: 'teacher01',
|
||||||
|
name: '测试教师',
|
||||||
|
profile: {
|
||||||
|
joinedAt: '2026-07-01',
|
||||||
|
qualifications: '教师资格证',
|
||||||
|
subjects: ['语文', '历史'],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
joinedAt: '2026-07-01',
|
||||||
|
qualifications: '教师资格证',
|
||||||
|
subjects: ['语文', '历史'],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty form values when the user has no profile', () => {
|
||||||
|
expect(userProfileResponseToFormValues({ profile: null })).toEqual({});
|
||||||
|
});
|
||||||
|
});
|
||||||
16
apps/admin/src/pages/Users/user-profile-form.ts
Normal file
16
apps/admin/src/pages/Users/user-profile-form.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export interface UserProfileFormValues {
|
||||||
|
joinedAt?: string;
|
||||||
|
qualifications?: string;
|
||||||
|
subjects?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserProfileResponse {
|
||||||
|
id?: number;
|
||||||
|
username?: string;
|
||||||
|
name?: string;
|
||||||
|
profile?: UserProfileFormValues | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const userProfileResponseToFormValues = (
|
||||||
|
response: UserProfileResponse,
|
||||||
|
): UserProfileFormValues => response.profile || {};
|
||||||
25
apps/admin/src/utils/notification-display.ts
Normal file
25
apps/admin/src/utils/notification-display.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export const notificationTypeLabels: Record<string, string> = {
|
||||||
|
bill_generated: '账单',
|
||||||
|
bill_paid: '账单',
|
||||||
|
check_in: '入住',
|
||||||
|
check_out: '退宿',
|
||||||
|
deposit_due: '押金',
|
||||||
|
deposit_refunded: '押金',
|
||||||
|
class_change: '班级',
|
||||||
|
schedule_conflict: '排课',
|
||||||
|
announcement: '公告',
|
||||||
|
};
|
||||||
|
|
||||||
|
const teacherRoleLabels: Record<string, string> = {
|
||||||
|
subject_teacher: '任课老师',
|
||||||
|
head_teacher: '班主任',
|
||||||
|
life_teacher: '生活老师',
|
||||||
|
academic_teacher: '学服老师',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function formatNotificationText(text: string): string {
|
||||||
|
return Object.entries(teacherRoleLabels).reduce(
|
||||||
|
(result, [roleType, label]) => result.replaceAll(roleType, label),
|
||||||
|
text,
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"author": "",
|
"author": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "SEED_DEV=true nest start --watch -p tsconfig.build.json",
|
"dev": "cross-env SEED_DEV=true nest start --watch -p tsconfig.build.json",
|
||||||
"build": "nest build -p tsconfig.build.json",
|
"build": "nest build -p tsconfig.build.json",
|
||||||
"start:dev": "nest start --watch",
|
"start:dev": "nest start --watch",
|
||||||
"format": "oxfmt",
|
"format": "oxfmt",
|
||||||
@@ -28,6 +28,7 @@
|
|||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.0.1",
|
||||||
"@nestjs/event-emitter": "^3.1.0",
|
"@nestjs/event-emitter": "^3.1.0",
|
||||||
"@nestjs/jwt": "^11.0.2",
|
"@nestjs/jwt": "^11.0.2",
|
||||||
|
"@nestjs/mapped-types": "^2.1.1",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/passport": "^11.0.5",
|
||||||
"@nestjs/platform-express": "^11.1.19",
|
"@nestjs/platform-express": "^11.1.19",
|
||||||
"@nestjs/schedule": "^6.1.3",
|
"@nestjs/schedule": "^6.1.3",
|
||||||
@@ -67,6 +68,7 @@
|
|||||||
"@types/passport-jwt": "^4.0.1",
|
"@types/passport-jwt": "^4.0.1",
|
||||||
"@types/passport-local": "^1.0.38",
|
"@types/passport-local": "^1.0.38",
|
||||||
"@types/supertest": "^7.0.0",
|
"@types/supertest": "^7.0.0",
|
||||||
|
"cross-env": "^10.1.0",
|
||||||
"eslint": "^9.18.0",
|
"eslint": "^9.18.0",
|
||||||
"globals": "^17.0.0",
|
"globals": "^17.0.0",
|
||||||
"jest": "^30.0.0",
|
"jest": "^30.0.0",
|
||||||
|
|||||||
31
apps/server/src/archive/archive-report.service.spec.ts
Normal file
31
apps/server/src/archive/archive-report.service.spec.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { ArchiveReportService } from './archive-report.service';
|
||||||
|
|
||||||
|
describe('ArchiveReportService retired profile fields', () => {
|
||||||
|
it('does not render the retired campus field in a student report', async () => {
|
||||||
|
const service = new ArchiveReportService(
|
||||||
|
{ findOne: jest.fn().mockResolvedValue({ campusLocation: '旧校区', grade: '高三' }) } as never,
|
||||||
|
{ find: jest.fn().mockResolvedValue([]) } as never,
|
||||||
|
{ find: jest.fn().mockResolvedValue([]) } as never,
|
||||||
|
{ find: jest.fn().mockResolvedValue([]) } as never,
|
||||||
|
{ findOne: jest.fn().mockResolvedValue(null) } as never,
|
||||||
|
{ find: jest.fn().mockResolvedValue([]) } as never,
|
||||||
|
{
|
||||||
|
findOne: jest.fn().mockResolvedValue({
|
||||||
|
id: 1,
|
||||||
|
name: '测试学生',
|
||||||
|
gender: '男',
|
||||||
|
phone: '',
|
||||||
|
ethnicity: '',
|
||||||
|
emergencyContact: '',
|
||||||
|
emergencyPhone: '',
|
||||||
|
}),
|
||||||
|
} as never,
|
||||||
|
);
|
||||||
|
|
||||||
|
const html = await service.generateReportHtml(1);
|
||||||
|
|
||||||
|
expect(html).not.toContain('旧校区');
|
||||||
|
expect(html).not.toContain('<span>校区</span>');
|
||||||
|
expect(html).toContain('高三');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -308,7 +308,6 @@ ${this.buildLearningAndResult(learnings, result, now)}
|
|||||||
<div class="summary-row"><span>民族</span><span>${this.esc(student.ethnicity || '-')}</span></div>
|
<div class="summary-row"><span>民族</span><span>${this.esc(student.ethnicity || '-')}</span></div>
|
||||||
<div class="summary-row"><span>紧急联系人</span><span>${this.esc(student.emergencyContact || '-')}</span></div>
|
<div class="summary-row"><span>紧急联系人</span><span>${this.esc(student.emergencyContact || '-')}</span></div>
|
||||||
<div class="summary-row"><span>紧急电话</span><span>${this.esc(student.emergencyPhone || '-')}</span></div>
|
<div class="summary-row"><span>紧急电话</span><span>${this.esc(student.emergencyPhone || '-')}</span></div>
|
||||||
<div class="summary-row"><span>校区</span><span>${this.esc(profile?.campusLocation || '-')}</span></div>
|
|
||||||
<div class="summary-row"><span>年级</span><span>${this.esc(profile?.grade || '-')}</span></div>
|
<div class="summary-row"><span>年级</span><span>${this.esc(profile?.grade || '-')}</span></div>
|
||||||
</div>
|
</div>
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|||||||
@@ -20,8 +20,11 @@ import { ArchiveService } from './archive.service';
|
|||||||
import {
|
import {
|
||||||
UpsertProfileDto,
|
UpsertProfileDto,
|
||||||
CreateEnrollmentDto,
|
CreateEnrollmentDto,
|
||||||
|
UpdateEnrollmentDto,
|
||||||
CreateExamScoreDto,
|
CreateExamScoreDto,
|
||||||
|
UpdateExamScoreDto,
|
||||||
CreateLearningRecordDto,
|
CreateLearningRecordDto,
|
||||||
|
UpdateLearningRecordDto,
|
||||||
UpsertResultDto,
|
UpsertResultDto,
|
||||||
} from './dto/archive.dto';
|
} from './dto/archive.dto';
|
||||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||||
@@ -110,7 +113,7 @@ export class ArchiveController {
|
|||||||
@RequirePermission('student:edit')
|
@RequirePermission('student:edit')
|
||||||
async updateEnrollment(
|
async updateEnrollment(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() dto: Partial<CreateEnrollmentDto>,
|
@Body() dto: UpdateEnrollmentDto,
|
||||||
@Request() req: AuthenticatedRequest,
|
@Request() req: AuthenticatedRequest,
|
||||||
) {
|
) {
|
||||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
@@ -174,7 +177,7 @@ export class ArchiveController {
|
|||||||
@RequirePermission('student:edit')
|
@RequirePermission('student:edit')
|
||||||
async updateExamScore(
|
async updateExamScore(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() dto: Partial<CreateExamScoreDto>,
|
@Body() dto: UpdateExamScoreDto,
|
||||||
@Request() req: AuthenticatedRequest,
|
@Request() req: AuthenticatedRequest,
|
||||||
) {
|
) {
|
||||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
@@ -238,7 +241,7 @@ export class ArchiveController {
|
|||||||
@RequirePermission('student:edit')
|
@RequirePermission('student:edit')
|
||||||
async updateLearningRecord(
|
async updateLearningRecord(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() dto: Partial<CreateLearningRecordDto>,
|
@Body() dto: UpdateLearningRecordDto,
|
||||||
@Request() req: AuthenticatedRequest,
|
@Request() req: AuthenticatedRequest,
|
||||||
) {
|
) {
|
||||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
@@ -330,12 +333,12 @@ export class ArchiveController {
|
|||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Res() res: Response,
|
@Res() res: Response,
|
||||||
) {
|
) {
|
||||||
const { fullPath, fileName, mimeType } = await this.archiveService.getAttachmentFile(+studentId, +id);
|
const { fullPath, fileName, mimeType } = await this.archiveService.getAttachmentFile(
|
||||||
res.setHeader('Content-Type', mimeType);
|
+studentId,
|
||||||
res.setHeader(
|
+id,
|
||||||
'Content-Disposition',
|
|
||||||
`inline; filename="${encodeURIComponent(fileName)}"`,
|
|
||||||
);
|
);
|
||||||
|
res.setHeader('Content-Type', mimeType);
|
||||||
|
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(fileName)}"`);
|
||||||
const stream = fs.createReadStream(fullPath);
|
const stream = fs.createReadStream(fullPath);
|
||||||
stream.pipe(res);
|
stream.pipe(res);
|
||||||
}
|
}
|
||||||
@@ -358,7 +361,6 @@ export class ArchiveController {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Get(':studentId/report-html')
|
@Get(':studentId/report-html')
|
||||||
@RequirePermission('student:view')
|
@RequirePermission('student:view')
|
||||||
async generateReportHtml(
|
async generateReportHtml(
|
||||||
|
|||||||
37
apps/server/src/archive/archive.service.spec.ts
Normal file
37
apps/server/src/archive/archive.service.spec.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { ArchiveService } from './archive.service';
|
||||||
|
|
||||||
|
describe('ArchiveService.getProfile', () => {
|
||||||
|
it('returns the admission archive under the public result field', async () => {
|
||||||
|
const student = { id: 7, name: '测试学生' };
|
||||||
|
const result = {
|
||||||
|
id: 3,
|
||||||
|
studentId: 7,
|
||||||
|
cultureFinalScore: 450,
|
||||||
|
admittedCollege: '测试学院',
|
||||||
|
};
|
||||||
|
|
||||||
|
const studentRepo = { findOne: jest.fn().mockResolvedValue(student) };
|
||||||
|
const profileRepo = { findOne: jest.fn().mockResolvedValue(null) };
|
||||||
|
const enrollmentRepo = { find: jest.fn().mockResolvedValue([]) };
|
||||||
|
const examScoreRepo = { find: jest.fn().mockResolvedValue([]) };
|
||||||
|
const learningRecordRepo = { find: jest.fn().mockResolvedValue([]) };
|
||||||
|
const resultRepo = { findOne: jest.fn().mockResolvedValue(result) };
|
||||||
|
const attachmentRepo = { find: jest.fn().mockResolvedValue([]) };
|
||||||
|
|
||||||
|
const service = new ArchiveService(
|
||||||
|
studentRepo as never,
|
||||||
|
profileRepo as never,
|
||||||
|
enrollmentRepo as never,
|
||||||
|
examScoreRepo as never,
|
||||||
|
learningRecordRepo as never,
|
||||||
|
resultRepo as never,
|
||||||
|
attachmentRepo as never,
|
||||||
|
{} as never,
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await service.getProfile(7);
|
||||||
|
|
||||||
|
expect(response).toMatchObject({ student, result });
|
||||||
|
expect(response).not.toHaveProperty('resultArchive');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -15,8 +15,11 @@ import { ArchiveAttachment } from '../entities/archive-attachment.entity';
|
|||||||
import {
|
import {
|
||||||
UpsertProfileDto,
|
UpsertProfileDto,
|
||||||
CreateEnrollmentDto,
|
CreateEnrollmentDto,
|
||||||
|
UpdateEnrollmentDto,
|
||||||
CreateExamScoreDto,
|
CreateExamScoreDto,
|
||||||
|
UpdateExamScoreDto,
|
||||||
CreateLearningRecordDto,
|
CreateLearningRecordDto,
|
||||||
|
UpdateLearningRecordDto,
|
||||||
UpsertResultDto,
|
UpsertResultDto,
|
||||||
} from './dto/archive.dto';
|
} from './dto/archive.dto';
|
||||||
|
|
||||||
@@ -44,7 +47,9 @@ export class ArchiveService {
|
|||||||
? path.resolve(process.cwd(), normalizedPath)
|
? path.resolve(process.cwd(), normalizedPath)
|
||||||
: path.resolve(this.uploadDir, normalizedPath);
|
: path.resolve(this.uploadDir, normalizedPath);
|
||||||
const allowedRoots = [this.uploadDir, path.resolve(process.cwd(), 'uploads', 'archive')];
|
const allowedRoots = [this.uploadDir, path.resolve(process.cwd(), 'uploads', 'archive')];
|
||||||
if (!allowedRoots.some((root) => fullPath === root || fullPath.startsWith(`${root}${path.sep}`))) {
|
if (
|
||||||
|
!allowedRoots.some((root) => fullPath === root || fullPath.startsWith(`${root}${path.sep}`))
|
||||||
|
) {
|
||||||
throw new BadRequestException('路径非法');
|
throw new BadRequestException('路径非法');
|
||||||
}
|
}
|
||||||
return fullPath;
|
return fullPath;
|
||||||
@@ -54,21 +59,15 @@ export class ArchiveService {
|
|||||||
const student = await this.studentRepo.findOne({ where: { id: studentId } });
|
const student = await this.studentRepo.findOne({ where: { id: studentId } });
|
||||||
if (!student) throw new NotFoundException('学生不存在');
|
if (!student) throw new NotFoundException('学生不存在');
|
||||||
|
|
||||||
const [
|
const [profileRaw, enrollments, examScores, learningRecords, resultArchive, attachments] =
|
||||||
profileRaw,
|
await Promise.all([
|
||||||
enrollments,
|
this.profileRepo.findOne({ where: { studentId } }),
|
||||||
examScores,
|
this.enrollmentRepo.find({ where: { studentId }, order: { createdAt: 'DESC' } }),
|
||||||
learningRecords,
|
this.examScoreRepo.find({ where: { studentId }, order: { examDate: 'DESC' } }),
|
||||||
resultArchive,
|
this.learningRecordRepo.find({ where: { studentId }, order: { recordDate: 'DESC' } }),
|
||||||
attachments,
|
this.resultRepo.findOne({ where: { studentId } }),
|
||||||
] = await Promise.all([
|
this.attachmentRepo.find({ where: { studentId }, order: { createdAt: 'DESC' } }),
|
||||||
this.profileRepo.findOne({ where: { studentId } }),
|
]);
|
||||||
this.enrollmentRepo.find({ where: { studentId }, order: { createdAt: 'DESC' } }),
|
|
||||||
this.examScoreRepo.find({ where: { studentId }, order: { examDate: 'DESC' } }),
|
|
||||||
this.learningRecordRepo.find({ where: { studentId }, order: { recordDate: 'DESC' } }),
|
|
||||||
this.resultRepo.findOne({ where: { studentId } }),
|
|
||||||
this.attachmentRepo.find({ where: { studentId }, order: { createdAt: 'DESC' } }),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
student,
|
student,
|
||||||
@@ -76,7 +75,7 @@ export class ArchiveService {
|
|||||||
enrollments,
|
enrollments,
|
||||||
examScores,
|
examScores,
|
||||||
learningRecords,
|
learningRecords,
|
||||||
resultArchive,
|
result: resultArchive,
|
||||||
attachments,
|
attachments,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -102,7 +101,7 @@ export class ArchiveService {
|
|||||||
return this.enrollmentRepo.save(entity);
|
return this.enrollmentRepo.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateEnrollment(id: number, dto: Partial<CreateEnrollmentDto>) {
|
async updateEnrollment(id: number, dto: UpdateEnrollmentDto) {
|
||||||
const entity = await this.enrollmentRepo.findOne({ where: { id } });
|
const entity = await this.enrollmentRepo.findOne({ where: { id } });
|
||||||
if (!entity) throw new NotFoundException('报名记录不存在');
|
if (!entity) throw new NotFoundException('报名记录不存在');
|
||||||
Object.assign(entity, dto);
|
Object.assign(entity, dto);
|
||||||
@@ -124,7 +123,7 @@ export class ArchiveService {
|
|||||||
return this.examScoreRepo.save(entity);
|
return this.examScoreRepo.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateExamScore(id: number, dto: Partial<CreateExamScoreDto>) {
|
async updateExamScore(id: number, dto: UpdateExamScoreDto) {
|
||||||
const entity = await this.examScoreRepo.findOne({ where: { id } });
|
const entity = await this.examScoreRepo.findOne({ where: { id } });
|
||||||
if (!entity) throw new NotFoundException('考试成绩不存在');
|
if (!entity) throw new NotFoundException('考试成绩不存在');
|
||||||
Object.assign(entity, dto);
|
Object.assign(entity, dto);
|
||||||
@@ -146,7 +145,7 @@ export class ArchiveService {
|
|||||||
return this.learningRecordRepo.save(entity);
|
return this.learningRecordRepo.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateLearningRecord(id: number, dto: Partial<CreateLearningRecordDto>) {
|
async updateLearningRecord(id: number, dto: UpdateLearningRecordDto) {
|
||||||
const entity = await this.learningRecordRepo.findOne({ where: { id } });
|
const entity = await this.learningRecordRepo.findOne({ where: { id } });
|
||||||
if (!entity) throw new NotFoundException('学习记录不存在');
|
if (!entity) throw new NotFoundException('学习记录不存在');
|
||||||
Object.assign(entity, dto);
|
Object.assign(entity, dto);
|
||||||
@@ -225,4 +224,3 @@ export class ArchiveService {
|
|||||||
return { message: '已删除' };
|
return { message: '已删除' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
57
apps/server/src/archive/dto/archive.dto.spec.ts
Normal file
57
apps/server/src/archive/dto/archive.dto.spec.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
|
import { plainToInstance } from 'class-transformer';
|
||||||
|
import { validate } from 'class-validator';
|
||||||
|
import {
|
||||||
|
UpdateEnrollmentDto,
|
||||||
|
UpdateExamScoreDto,
|
||||||
|
UpdateLearningRecordDto,
|
||||||
|
UpsertProfileDto,
|
||||||
|
} from './archive.dto';
|
||||||
|
|
||||||
|
const pipe = new ValidationPipe({ transform: true, whitelist: true });
|
||||||
|
|
||||||
|
async function transform<T extends object>(metatype: new () => T, value: unknown) {
|
||||||
|
return pipe.transform(value, { type: 'body', metatype });
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('UpsertProfileDto retired fields', () => {
|
||||||
|
it('removes the retired campusLocation field under whitelist validation', async () => {
|
||||||
|
const dto = plainToInstance(UpsertProfileDto, {
|
||||||
|
grade: '高三',
|
||||||
|
campusLocation: '旧校区',
|
||||||
|
});
|
||||||
|
|
||||||
|
await validate(dto, { whitelist: true });
|
||||||
|
|
||||||
|
expect(dto).toMatchObject({ grade: '高三' });
|
||||||
|
expect(dto).not.toHaveProperty('campusLocation');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('archive update DTOs', () => {
|
||||||
|
it('allows partial enrollment updates and strips unknown fields', async () => {
|
||||||
|
await expect(
|
||||||
|
transform(UpdateEnrollmentDto, { className: '新班级', ignored: 'value' }),
|
||||||
|
).resolves.toEqual(expect.objectContaining({ className: '新班级' }));
|
||||||
|
const result = await transform(UpdateEnrollmentDto, {
|
||||||
|
className: '新班级',
|
||||||
|
ignored: 'value',
|
||||||
|
});
|
||||||
|
expect(result).not.toHaveProperty('ignored');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('retains create DTO validation rules for exam scores', async () => {
|
||||||
|
await expect(transform(UpdateExamScoreDto, { score: '90' })).rejects.toThrow();
|
||||||
|
await expect(transform(UpdateExamScoreDto, { score: 90 })).resolves.toMatchObject({
|
||||||
|
score: 90,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('retains create DTO date validation for learning records', async () => {
|
||||||
|
await expect(
|
||||||
|
transform(UpdateLearningRecordDto, { recordDate: 'not-a-date' }),
|
||||||
|
).rejects.toThrow();
|
||||||
|
await expect(transform(UpdateLearningRecordDto, {})).resolves.toEqual({});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
import { IsOptional, IsString, IsNumber, IsDateString } from 'class-validator';
|
import { IsOptional, IsString, IsNumber, IsDateString } from 'class-validator';
|
||||||
|
|
||||||
export class UpsertProfileDto {
|
export class UpsertProfileDto {
|
||||||
@@ -5,7 +6,6 @@ export class UpsertProfileDto {
|
|||||||
@IsOptional() @IsString() targetMajor?: string;
|
@IsOptional() @IsString() targetMajor?: string;
|
||||||
@IsOptional() @IsString() subjectDirection?: string;
|
@IsOptional() @IsString() subjectDirection?: string;
|
||||||
@IsOptional() @IsString() grade?: string;
|
@IsOptional() @IsString() grade?: string;
|
||||||
@IsOptional() @IsString() campusLocation?: string;
|
|
||||||
@IsOptional() @IsDateString() profileDate?: string;
|
@IsOptional() @IsDateString() profileDate?: string;
|
||||||
@IsOptional() @IsString() notes?: string;
|
@IsOptional() @IsString() notes?: string;
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,8 @@ export class CreateEnrollmentDto {
|
|||||||
@IsOptional() @IsString() status?: string;
|
@IsOptional() @IsString() status?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UpdateEnrollmentDto extends PartialType(CreateEnrollmentDto) {}
|
||||||
|
|
||||||
export class CreateExamScoreDto {
|
export class CreateExamScoreDto {
|
||||||
@IsString() examType: string;
|
@IsString() examType: string;
|
||||||
@IsOptional() @IsString() examName?: string;
|
@IsOptional() @IsString() examName?: string;
|
||||||
@@ -32,6 +34,8 @@ export class CreateExamScoreDto {
|
|||||||
@IsOptional() @IsNumber() enrollmentId?: number;
|
@IsOptional() @IsNumber() enrollmentId?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UpdateExamScoreDto extends PartialType(CreateExamScoreDto) {}
|
||||||
|
|
||||||
export class CreateLearningRecordDto {
|
export class CreateLearningRecordDto {
|
||||||
@IsDateString() recordDate: string;
|
@IsDateString() recordDate: string;
|
||||||
@IsString() recordType: string;
|
@IsString() recordType: string;
|
||||||
@@ -40,6 +44,8 @@ export class CreateLearningRecordDto {
|
|||||||
@IsOptional() @IsString() nextStep?: string;
|
@IsOptional() @IsString() nextStep?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UpdateLearningRecordDto extends PartialType(CreateLearningRecordDto) {}
|
||||||
|
|
||||||
export class UpsertResultDto {
|
export class UpsertResultDto {
|
||||||
@IsOptional() @IsNumber() cultureFinalScore?: number;
|
@IsOptional() @IsNumber() cultureFinalScore?: number;
|
||||||
@IsOptional() @IsNumber() professionalFinalScore?: number;
|
@IsOptional() @IsNumber() professionalFinalScore?: number;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ describe('DingTalkService — attendance records', () => {
|
|||||||
service = new DingTalkService({} as never, {} as never);
|
service = new DingTalkService({} as never, {} as never);
|
||||||
Object.assign(service, {
|
Object.assign(service, {
|
||||||
accessToken: 'test-token',
|
accessToken: 'test-token',
|
||||||
|
accessTokenCredentialKey: 'test-app-key:test-app-secret',
|
||||||
tokenExpiresAt: Date.now() + 3_600_000,
|
tokenExpiresAt: Date.now() + 3_600_000,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { ClassesService } from './classes.service';
|
||||||
|
import { ClassStudent } from '../entities';
|
||||||
|
|
||||||
|
describe('ClassesService — DingTalk class import membership lifecycle', () => {
|
||||||
|
it('reactivates left memberships and skips active memberships', async () => {
|
||||||
|
const left = {
|
||||||
|
classId: 3,
|
||||||
|
studentId: 8,
|
||||||
|
status: 'left',
|
||||||
|
joinDate: '2026-01-01',
|
||||||
|
leaveDate: '2026-02-01',
|
||||||
|
} as ClassStudent;
|
||||||
|
const active = { classId: 3, studentId: 9, status: 'active' } as ClassStudent;
|
||||||
|
const classStudentRepo = {
|
||||||
|
find: jest.fn().mockResolvedValue([left, active]),
|
||||||
|
create: jest.fn().mockImplementation((value: Partial<ClassStudent>) => value),
|
||||||
|
save: jest.fn().mockImplementation(async (value: ClassStudent[]) => value),
|
||||||
|
};
|
||||||
|
const service = new ClassesService(
|
||||||
|
{ findOne: jest.fn().mockResolvedValue({ id: 3 }) } as never,
|
||||||
|
classStudentRepo as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
{ create: jest.fn(), save: jest.fn() } as never,
|
||||||
|
{
|
||||||
|
find: jest.fn().mockResolvedValue([
|
||||||
|
{ dingUserId: 'd8', studentId: 8 },
|
||||||
|
{ dingUserId: 'd9', studentId: 9 },
|
||||||
|
]),
|
||||||
|
create: jest.fn(),
|
||||||
|
save: jest.fn(),
|
||||||
|
} as never,
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await service.batchImportStudents(3, [
|
||||||
|
{ dingUserId: 'd8', name: '学生8' },
|
||||||
|
{ dingUserId: 'd9', name: '学生9' },
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(result).toEqual({ imported: 1, skipped: 1 });
|
||||||
|
expect(left).toMatchObject({ status: 'active', leaveDate: null });
|
||||||
|
expect(left.joinDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||||
|
expect(classStudentRepo.save).toHaveBeenCalledWith([left]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -29,6 +29,7 @@ import { extractRequestInfo } from '../common/request-utils';
|
|||||||
import { RequirePermission } from '../auth/decorators/permission.decorator';
|
import { RequirePermission } from '../auth/decorators/permission.decorator';
|
||||||
import { NotificationsService } from '../notifications/notifications.service';
|
import { NotificationsService } from '../notifications/notifications.service';
|
||||||
import { NotificationType } from '../entities/notification.entity';
|
import { NotificationType } from '../entities/notification.entity';
|
||||||
|
import { TeacherRoleType } from '../entities';
|
||||||
import * as ExcelJS from 'exceljs';
|
import * as ExcelJS from 'exceljs';
|
||||||
import { AuthorizationService, CaslAction, SubjectName, AuthenticatedUser } from '../authorization';
|
import { AuthorizationService, CaslAction, SubjectName, AuthenticatedUser } from '../authorization';
|
||||||
|
|
||||||
@@ -36,6 +37,13 @@ interface AuthenticatedRequest {
|
|||||||
user: AuthenticatedUser;
|
user: AuthenticatedUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const teacherRoleLabels: Record<string, string> = {
|
||||||
|
[TeacherRoleType.SUBJECT_TEACHER]: '任课老师',
|
||||||
|
[TeacherRoleType.HEAD_TEACHER]: '班主任',
|
||||||
|
[TeacherRoleType.LIFE_TEACHER]: '生活老师',
|
||||||
|
[TeacherRoleType.ACADEMIC_TEACHER]: '学服老师',
|
||||||
|
};
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Controller('classes')
|
@Controller('classes')
|
||||||
export class ClassesController {
|
export class ClassesController {
|
||||||
@@ -302,7 +310,7 @@ export class ClassesController {
|
|||||||
recipientIds: [dto.userId],
|
recipientIds: [dto.userId],
|
||||||
type: NotificationType.CLASS_CHANGE,
|
type: NotificationType.CLASS_CHANGE,
|
||||||
title: '班级分配',
|
title: '班级分配',
|
||||||
content: `您已被分配到班级担任${dto.roleType}角色`,
|
content: `您已被分配到班级担任${teacherRoleLabels[dto.roleType] ?? dto.roleType}角色`,
|
||||||
});
|
});
|
||||||
} catch {}
|
} catch {}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
95
apps/server/src/classes/classes.membership.spec.ts
Normal file
95
apps/server/src/classes/classes.membership.spec.ts
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { ClassesService } from './classes.service';
|
||||||
|
import { ClassStudent } from '../entities';
|
||||||
|
|
||||||
|
function createService(
|
||||||
|
classStudentRepo: Record<string, jest.Mock>,
|
||||||
|
classRepo: Record<string, jest.Mock> = { findOne: jest.fn().mockResolvedValue({ id: 3 }) },
|
||||||
|
studentRepo: Record<string, jest.Mock> = {
|
||||||
|
find: jest.fn().mockResolvedValue([{ id: 8 }, { id: 9 }, { id: 10 }]),
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
return new ClassesService(
|
||||||
|
classRepo as never,
|
||||||
|
classStudentRepo as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
{} as never,
|
||||||
|
studentRepo as never,
|
||||||
|
{} as never,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('ClassesService — student membership lifecycle', () => {
|
||||||
|
it('marks an active membership as left instead of deleting it', async () => {
|
||||||
|
const membership = {
|
||||||
|
classId: 3,
|
||||||
|
studentId: 8,
|
||||||
|
status: 'active',
|
||||||
|
leaveDate: null,
|
||||||
|
} as ClassStudent;
|
||||||
|
const repo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue(membership),
|
||||||
|
save: jest.fn().mockImplementation(async (value: ClassStudent) => value),
|
||||||
|
};
|
||||||
|
const service = createService(repo);
|
||||||
|
|
||||||
|
await service.removeStudent(3, 8);
|
||||||
|
|
||||||
|
expect(membership.status).toBe('left');
|
||||||
|
expect(membership.leaveDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||||
|
expect(repo.save).toHaveBeenCalledWith(membership);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects removing an already-left membership', async () => {
|
||||||
|
const repo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue({ status: 'left' }),
|
||||||
|
save: jest.fn(),
|
||||||
|
};
|
||||||
|
const service = createService(repo);
|
||||||
|
|
||||||
|
await expect(service.removeStudent(3, 8)).rejects.toBeInstanceOf(BadRequestException);
|
||||||
|
expect(repo.save).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects removing a student without a membership', async () => {
|
||||||
|
const repo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue(null),
|
||||||
|
save: jest.fn(),
|
||||||
|
};
|
||||||
|
const service = createService(repo);
|
||||||
|
|
||||||
|
await expect(service.removeStudent(3, 8)).rejects.toBeInstanceOf(NotFoundException);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reactivates left memberships, creates new ones, and skips active ones', async () => {
|
||||||
|
const left = {
|
||||||
|
id: 1,
|
||||||
|
classId: 3,
|
||||||
|
studentId: 8,
|
||||||
|
status: 'left',
|
||||||
|
joinDate: '2026-01-01',
|
||||||
|
leaveDate: '2026-02-01',
|
||||||
|
} as ClassStudent;
|
||||||
|
const active = { id: 2, classId: 3, studentId: 9, status: 'active' } as ClassStudent;
|
||||||
|
const repo = {
|
||||||
|
find: jest.fn().mockResolvedValue([left, active]),
|
||||||
|
create: jest.fn().mockImplementation((value: Partial<ClassStudent>) => value),
|
||||||
|
save: jest.fn().mockImplementation(async (value: ClassStudent[]) => value),
|
||||||
|
};
|
||||||
|
const service = createService(repo);
|
||||||
|
|
||||||
|
const result = await service.addStudents(3, [8, 9, 10, 10]);
|
||||||
|
|
||||||
|
expect(result).toEqual({ added: 2, skipped: 1 });
|
||||||
|
expect(left).toMatchObject({ status: 'active', leaveDate: null });
|
||||||
|
expect(left.joinDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||||
|
expect(repo.create).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ classId: 3, studentId: 10, status: 'active' }),
|
||||||
|
);
|
||||||
|
expect(repo.save).toHaveBeenCalledWith(
|
||||||
|
expect.arrayContaining([left, expect.objectContaining({ studentId: 10 })]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -227,34 +227,45 @@ export class ClassesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Fetch existing class-student links in one query
|
// 3. Fetch existing class-student links in one query
|
||||||
const allStudentIds = Array.from(dingToStudentId.values());
|
const allStudentIds = Array.from(new Set(dingToStudentId.values()));
|
||||||
const alreadyInClass = new Set<number>();
|
const existingClassStudents =
|
||||||
if (allStudentIds.length > 0) {
|
allStudentIds.length > 0
|
||||||
const existingClassStudents = await this.classStudentRepo.find({
|
? await this.classStudentRepo.find({
|
||||||
where: { classId, studentId: In(allStudentIds) },
|
where: { classId, studentId: In(allStudentIds) },
|
||||||
});
|
})
|
||||||
for (const cs of existingClassStudents) {
|
: [];
|
||||||
alreadyInClass.add(cs.studentId);
|
const existingByStudentId = new Map(
|
||||||
|
existingClassStudents.map((classStudent) => [classStudent.studentId, classStudent]),
|
||||||
|
);
|
||||||
|
const today = new Date().toISOString().slice(0, 10);
|
||||||
|
let skipped = 0;
|
||||||
|
const memberships = allStudentIds.flatMap((studentId) => {
|
||||||
|
const existing = existingByStudentId.get(studentId);
|
||||||
|
if (existing?.status === 'active') {
|
||||||
|
skipped++;
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
if (existing) {
|
||||||
|
existing.status = 'active';
|
||||||
// 4. Batch insert new class-student records
|
existing.joinDate = today;
|
||||||
const newClassStudents = allStudentIds
|
existing.leaveDate = null;
|
||||||
.filter((sid) => !alreadyInClass.has(sid))
|
return [existing];
|
||||||
.map((studentId) =>
|
}
|
||||||
|
return [
|
||||||
this.classStudentRepo.create({
|
this.classStudentRepo.create({
|
||||||
classId,
|
classId,
|
||||||
studentId,
|
studentId,
|
||||||
status: 'active',
|
status: 'active',
|
||||||
joinDate: new Date().toISOString().slice(0, 10),
|
joinDate: today,
|
||||||
}),
|
}),
|
||||||
);
|
];
|
||||||
|
});
|
||||||
|
|
||||||
if (newClassStudents.length > 0) {
|
if (memberships.length > 0) {
|
||||||
await this.classStudentRepo.save(newClassStudents);
|
await this.classStudentRepo.save(memberships);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { imported: newClassStudents.length, skipped: alreadyInClass.size };
|
return { imported: memberships.length, skipped };
|
||||||
}
|
}
|
||||||
async update(id: number, dto: UpdateClassDto) {
|
async update(id: number, dto: UpdateClassDto) {
|
||||||
const cls = await this.classRepo.findOne({ where: { id } });
|
const cls = await this.classRepo.findOne({ where: { id } });
|
||||||
@@ -315,26 +326,61 @@ export class ClassesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async addStudents(classId: number, studentIds: number[]) {
|
async addStudents(classId: number, studentIds: number[]) {
|
||||||
|
const uniqueStudentIds = [...new Set(studentIds)];
|
||||||
|
if (uniqueStudentIds.length === 0) return { added: 0, skipped: 0 };
|
||||||
|
|
||||||
|
const cls = await this.classRepo.findOne({ where: { id: classId } });
|
||||||
|
if (!cls) throw new NotFoundException('班级不存在');
|
||||||
|
|
||||||
|
const students = await this.studentRepo.find({ where: { id: In(uniqueStudentIds) } });
|
||||||
|
if (students.length !== uniqueStudentIds.length) {
|
||||||
|
throw new NotFoundException('部分学生不存在');
|
||||||
|
}
|
||||||
|
|
||||||
const existing = await this.classStudentRepo.find({
|
const existing = await this.classStudentRepo.find({
|
||||||
where: { classId, studentId: In(studentIds) },
|
where: { classId, studentId: In(uniqueStudentIds) },
|
||||||
});
|
});
|
||||||
const existingIds = new Set(existing.map((e) => e.studentId));
|
const existingByStudentId = new Map(
|
||||||
const newIds = studentIds.filter((id) => !existingIds.has(id));
|
existing.map((classStudent) => [classStudent.studentId, classStudent]),
|
||||||
|
|
||||||
const entries = newIds.map((sid) =>
|
|
||||||
this.classStudentRepo.create({
|
|
||||||
classId,
|
|
||||||
studentId: sid,
|
|
||||||
joinDate: new Date().toISOString().split('T')[0],
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
if (entries.length) await this.classStudentRepo.save(entries);
|
const today = new Date().toISOString().split('T')[0];
|
||||||
|
let skipped = 0;
|
||||||
|
const memberships = uniqueStudentIds.flatMap((studentId) => {
|
||||||
|
const current = existingByStudentId.get(studentId);
|
||||||
|
if (current?.status === 'active') {
|
||||||
|
skipped++;
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if (current) {
|
||||||
|
current.status = 'active';
|
||||||
|
current.joinDate = today;
|
||||||
|
current.leaveDate = null;
|
||||||
|
return [current];
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
this.classStudentRepo.create({
|
||||||
|
classId,
|
||||||
|
studentId,
|
||||||
|
status: 'active',
|
||||||
|
joinDate: today,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
if (memberships.length) await this.classStudentRepo.save(memberships);
|
||||||
|
|
||||||
return { added: entries.length, skipped: studentIds.length - entries.length };
|
return { added: memberships.length, skipped };
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeStudent(classId: number, studentId: number) {
|
async removeStudent(classId: number, studentId: number) {
|
||||||
await this.classStudentRepo.delete({ classId, studentId });
|
const membership = await this.classStudentRepo.findOne({
|
||||||
|
where: { classId, studentId },
|
||||||
|
});
|
||||||
|
if (!membership) throw new NotFoundException('学生不在该班级');
|
||||||
|
if (membership.status !== 'active') throw new BadRequestException('学生已离班');
|
||||||
|
|
||||||
|
membership.status = 'left';
|
||||||
|
membership.leaveDate = new Date().toISOString().split('T')[0];
|
||||||
|
await this.classStudentRepo.save(membership);
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -137,6 +137,42 @@ export class ClassroomRentalsController {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put(':id/cancel')
|
||||||
|
@RequirePermission('rental:edit')
|
||||||
|
async cancel(@Param('id') id: string, @Request() req: any) {
|
||||||
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
|
const result = await this.service.cancel(+id);
|
||||||
|
await this.logService.log({
|
||||||
|
userId: req.user?.id,
|
||||||
|
username: req.user?.username,
|
||||||
|
module: '教室租赁',
|
||||||
|
action: '取消租赁',
|
||||||
|
targetId: +id,
|
||||||
|
targetType: 'classroom-rental',
|
||||||
|
ipAddress,
|
||||||
|
userAgent,
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put(':id/end')
|
||||||
|
@RequirePermission('rental:edit')
|
||||||
|
async end(@Param('id') id: string, @Request() req: any) {
|
||||||
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
|
const result = await this.service.end(+id);
|
||||||
|
await this.logService.log({
|
||||||
|
userId: req.user?.id,
|
||||||
|
username: req.user?.username,
|
||||||
|
module: '教室租赁',
|
||||||
|
action: '结束租赁',
|
||||||
|
targetId: +id,
|
||||||
|
targetType: 'classroom-rental',
|
||||||
|
ipAddress,
|
||||||
|
userAgent,
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@RequirePermission('rental:delete')
|
@RequirePermission('rental:delete')
|
||||||
async remove(@Param('id') id: string, @Request() req: any) {
|
async remove(@Param('id') id: string, @Request() req: any) {
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
startDate: '2026-03-01',
|
startDate: '2026-03-01',
|
||||||
endDate: '2026-03-31',
|
endDate: '2026-03-31',
|
||||||
};
|
};
|
||||||
const classroom = { id: 1, departmentId: 10 } as Classroom;
|
const classroom = { id: 1, status: 'available' } as Classroom;
|
||||||
const hostOrganization = {
|
const hostOrganization = {
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'Host',
|
name: 'Host',
|
||||||
@@ -315,8 +315,8 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
id: 1,
|
id: 1,
|
||||||
classroomId: 1,
|
classroomId: 1,
|
||||||
lesseeOrganizationId: 2,
|
lesseeOrganizationId: 2,
|
||||||
startDate: '2026-03-01',
|
startDate: '2026-07-01',
|
||||||
endDate: '2026-03-31',
|
endDate: '2099-03-31',
|
||||||
status: 'active',
|
status: 'active',
|
||||||
notes: '',
|
notes: '',
|
||||||
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
||||||
@@ -324,8 +324,8 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
} as ClassroomRental;
|
} as ClassroomRental;
|
||||||
const updatedRental = {
|
const updatedRental = {
|
||||||
...existingRental,
|
...existingRental,
|
||||||
startDate: '2026-04-01',
|
startDate: '2026-08-01',
|
||||||
endDate: '2026-04-30',
|
endDate: '2099-04-30',
|
||||||
};
|
};
|
||||||
const existingSchedule = {
|
const existingSchedule = {
|
||||||
id: 50,
|
id: 50,
|
||||||
@@ -339,12 +339,12 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
scheduleRepo.createQueryBuilder.mockReturnValue(mockQueryBuilder<ClassSchedule>([]));
|
scheduleRepo.createQueryBuilder.mockReturnValue(mockQueryBuilder<ClassSchedule>([]));
|
||||||
scheduleRepo.findOne.mockResolvedValue(existingSchedule);
|
scheduleRepo.findOne.mockResolvedValue(existingSchedule);
|
||||||
|
|
||||||
const dto: UpdateRentalDto = { startDate: '2026-04-01', endDate: '2026-04-30' };
|
const dto: UpdateRentalDto = { startDate: '2026-08-01', endDate: '2099-04-30' };
|
||||||
await service.update(1, dto);
|
await service.update(1, dto);
|
||||||
|
|
||||||
expect(rentalRepo.update).toHaveBeenCalledWith(
|
expect(rentalRepo.update).toHaveBeenCalledWith(
|
||||||
1,
|
1,
|
||||||
expect.objectContaining({ startDate: '2026-04-01', endDate: '2026-04-30' }),
|
expect.objectContaining({ startDate: '2026-08-01', endDate: '2099-04-30' }),
|
||||||
);
|
);
|
||||||
expect(scheduleRepo.update).toHaveBeenCalledWith(
|
expect(scheduleRepo.update).toHaveBeenCalledWith(
|
||||||
50,
|
50,
|
||||||
@@ -352,8 +352,8 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
scheduleType: 'RENTAL',
|
scheduleType: 'RENTAL',
|
||||||
rentalId: 1,
|
rentalId: 1,
|
||||||
classroomId: 1,
|
classroomId: 1,
|
||||||
startDate: '2026-04-01',
|
startDate: '2026-08-01',
|
||||||
endDate: '2026-04-30',
|
endDate: '2099-04-30',
|
||||||
status: 'active',
|
status: 'active',
|
||||||
subject: 'Organization A 租赁',
|
subject: 'Organization A 租赁',
|
||||||
}),
|
}),
|
||||||
@@ -362,27 +362,59 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
expect(scheduleRepo.delete).not.toHaveBeenCalled();
|
expect(scheduleRepo.delete).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deletes the RENTAL schedule row when status changes to cancelled', async () => {
|
it('deletes the RENTAL schedule row when the rental is cancelled', async () => {
|
||||||
const rental = {
|
const rental = {
|
||||||
id: 1,
|
id: 1,
|
||||||
classroomId: 1,
|
classroomId: 1,
|
||||||
lesseeOrganizationId: 2,
|
lesseeOrganizationId: 2,
|
||||||
startDate: '2026-03-01',
|
startDate: '2026-07-01',
|
||||||
endDate: '2026-03-31',
|
endDate: '2099-03-31',
|
||||||
status: 'active',
|
status: 'active',
|
||||||
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
||||||
} as ClassroomRental;
|
} as ClassroomRental;
|
||||||
const cancelledRental = { ...rental, status: 'cancelled' };
|
const cancelledRental = { ...rental, status: 'cancelled' } as ClassroomRental;
|
||||||
|
|
||||||
rentalRepo.findOne.mockResolvedValueOnce(rental).mockResolvedValueOnce(cancelledRental);
|
rentalRepo.findOne.mockResolvedValueOnce(rental).mockResolvedValueOnce(cancelledRental);
|
||||||
|
|
||||||
await service.update(1, { status: 'cancelled' });
|
await service.cancel(1);
|
||||||
|
|
||||||
expect(rentalRepo.update).toHaveBeenCalledWith(1, { status: 'cancelled' });
|
expect(rentalRepo.update).toHaveBeenCalledWith(1, { status: 'cancelled' });
|
||||||
expect(scheduleRepo.delete).toHaveBeenCalledWith({ rentalId: 1, scheduleType: 'RENTAL' });
|
expect(scheduleRepo.delete).toHaveBeenCalledWith({ rentalId: 1, scheduleType: 'RENTAL' });
|
||||||
expect(scheduleRepo.findOne).not.toHaveBeenCalled();
|
});
|
||||||
expect(scheduleRepo.update).not.toHaveBeenCalled();
|
});
|
||||||
expect(scheduleRepo.create).not.toHaveBeenCalled();
|
|
||||||
|
describe('lifecycle actions', () => {
|
||||||
|
it('ends an active rental and shortens a future end date', async () => {
|
||||||
|
const rental = {
|
||||||
|
id: 1,
|
||||||
|
classroomId: 1,
|
||||||
|
startDate: '2026-07-01',
|
||||||
|
endDate: '2099-12-31',
|
||||||
|
status: 'active',
|
||||||
|
lesseeOrganization: { name: 'Organization A' },
|
||||||
|
} as ClassroomRental;
|
||||||
|
const ended = { ...rental, status: 'ended', endDate: '2026-07-13' } as ClassroomRental;
|
||||||
|
rentalRepo.findOne.mockResolvedValueOnce(rental).mockResolvedValueOnce(ended);
|
||||||
|
scheduleRepo.findOne.mockResolvedValue({ id: 50 } as ClassSchedule);
|
||||||
|
|
||||||
|
await service.end(1);
|
||||||
|
|
||||||
|
expect(rentalRepo.update).toHaveBeenCalledWith(
|
||||||
|
1,
|
||||||
|
expect.objectContaining({ status: 'ended' }),
|
||||||
|
);
|
||||||
|
expect(scheduleRepo.update).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects ending a future rental', async () => {
|
||||||
|
rentalRepo.findOne.mockResolvedValue({
|
||||||
|
id: 1,
|
||||||
|
startDate: '2099-01-01',
|
||||||
|
endDate: '2099-12-31',
|
||||||
|
status: 'active',
|
||||||
|
} as ClassroomRental);
|
||||||
|
|
||||||
|
await expect(service.end(1)).rejects.toThrow('租赁尚未开始');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -394,7 +426,7 @@ describe('ClassroomRentalsService — rental schedule sync', () => {
|
|||||||
lesseeOrganizationId: 2,
|
lesseeOrganizationId: 2,
|
||||||
startDate: '2026-03-01',
|
startDate: '2026-03-01',
|
||||||
endDate: '2026-03-31',
|
endDate: '2026-03-31',
|
||||||
status: 'active',
|
status: 'cancelled',
|
||||||
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
lesseeOrganization: { id: 2, name: 'Organization A' } as Organization,
|
||||||
} as ClassroomRental;
|
} as ClassroomRental;
|
||||||
|
|
||||||
@@ -419,7 +451,7 @@ describe('ClassroomRentalsService — organization roles', () => {
|
|||||||
createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder<ClassroomRental>([])),
|
createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder<ClassroomRental>([])),
|
||||||
} as any;
|
} as any;
|
||||||
const classroomRepo = {
|
const classroomRepo = {
|
||||||
findOne: jest.fn().mockResolvedValue({ id: 1, departmentId: 10 }),
|
findOne: jest.fn().mockResolvedValue({ id: 1, status: 'available' }),
|
||||||
} as any;
|
} as any;
|
||||||
const organizationRepo = {
|
const organizationRepo = {
|
||||||
findOne: jest
|
findOne: jest
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository, Not, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
import { Repository, Not, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
||||||
import { ClassroomRental } from '../entities/classroom-rental.entity';
|
import { ClassroomRental, ClassroomRentalStatus } from '../entities/classroom-rental.entity';
|
||||||
import { Classroom } from '../entities/classroom.entity';
|
import { Classroom, ClassroomStatus } from '../entities/classroom.entity';
|
||||||
import { Organization } from '../entities/organization.entity';
|
import { Organization } from '../entities/organization.entity';
|
||||||
import { ClassSchedule } from '../entities/class-schedule.entity';
|
import { ClassSchedule } from '../entities/class-schedule.entity';
|
||||||
import { CreateRentalDto, UpdateRentalDto } from './dto/rental.dto';
|
import { CreateRentalDto, UpdateRentalDto } from './dto/rental.dto';
|
||||||
@@ -70,8 +70,11 @@ export class ClassroomRentalsService {
|
|||||||
const last = `${y}-${String(m).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`;
|
const last = `${y}-${String(m).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`;
|
||||||
qb.andWhere('r.startDate <= :last AND r.endDate >= :first', { first, last });
|
qb.andWhere('r.startDate <= :last AND r.endDate >= :first', { first, last });
|
||||||
}
|
}
|
||||||
if (!query?.includeEnded) qb.andWhere('r.status != :cancelled', { cancelled: 'cancelled' });
|
if (!query?.includeEnded) {
|
||||||
return qb.getMany();
|
qb.andWhere('r.status = :active', { active: ClassroomRentalStatus.ACTIVE });
|
||||||
|
}
|
||||||
|
const rentals = await qb.getMany();
|
||||||
|
return rentals.map((rental) => this.withEffectiveStatus(rental));
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: number) {
|
async findOne(id: number) {
|
||||||
@@ -80,7 +83,7 @@ export class ClassroomRentalsService {
|
|||||||
relations: ['classroom', 'lessorOrganization', 'lesseeOrganization'],
|
relations: ['classroom', 'lessorOrganization', 'lesseeOrganization'],
|
||||||
});
|
});
|
||||||
if (!rental) throw new NotFoundException('租赁订单不存在');
|
if (!rental) throw new NotFoundException('租赁订单不存在');
|
||||||
return rental;
|
return this.withEffectiveStatus(rental);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUnavailableDates(classroomId: number, year: number, month: number, excludeId?: number) {
|
async getUnavailableDates(classroomId: number, year: number, month: number, excludeId?: number) {
|
||||||
@@ -93,7 +96,7 @@ export class ClassroomRentalsService {
|
|||||||
where: {
|
where: {
|
||||||
...(excludeId ? { id: Not(excludeId) } : {}),
|
...(excludeId ? { id: Not(excludeId) } : {}),
|
||||||
classroomId,
|
classroomId,
|
||||||
status: Not('cancelled'),
|
status: ClassroomRentalStatus.ACTIVE,
|
||||||
startDate: LessThanOrEqual(monthEnd),
|
startDate: LessThanOrEqual(monthEnd),
|
||||||
endDate: MoreThanOrEqual(monthStart),
|
endDate: MoreThanOrEqual(monthStart),
|
||||||
},
|
},
|
||||||
@@ -101,7 +104,7 @@ export class ClassroomRentalsService {
|
|||||||
this.scheduleRepo.find({
|
this.scheduleRepo.find({
|
||||||
where: {
|
where: {
|
||||||
classroomId,
|
classroomId,
|
||||||
status: 'active',
|
status: ClassroomRentalStatus.ACTIVE,
|
||||||
scheduleType: 'INTERNAL',
|
scheduleType: 'INTERNAL',
|
||||||
startDate: LessThanOrEqual(monthEnd),
|
startDate: LessThanOrEqual(monthEnd),
|
||||||
endDate: MoreThanOrEqual(monthStart),
|
endDate: MoreThanOrEqual(monthStart),
|
||||||
@@ -133,7 +136,7 @@ export class ClassroomRentalsService {
|
|||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.leftJoinAndSelect('r.lesseeOrganization', 'lesseeOrganization')
|
.leftJoinAndSelect('r.lesseeOrganization', 'lesseeOrganization')
|
||||||
.where('r.classroomId = :cid', { cid: classroomId })
|
.where('r.classroomId = :cid', { cid: classroomId })
|
||||||
.andWhere('r.status != :cancelled', { cancelled: 'cancelled' })
|
.andWhere('r.status = :active', { active: ClassroomRentalStatus.ACTIVE })
|
||||||
.andWhere('r.startDate <= :end', { end: endDate })
|
.andWhere('r.startDate <= :end', { end: endDate })
|
||||||
.andWhere('r.endDate >= :start', { start: startDate });
|
.andWhere('r.endDate >= :start', { start: startDate });
|
||||||
if (excludeId) qb.andWhere('r.id != :excludeId', { excludeId });
|
if (excludeId) qb.andWhere('r.id != :excludeId', { excludeId });
|
||||||
@@ -222,6 +225,9 @@ export class ClassroomRentalsService {
|
|||||||
if (dto.startDate > dto.endDate) throw new BadRequestException('起始日期不能晚于结束日期');
|
if (dto.startDate > dto.endDate) throw new BadRequestException('起始日期不能晚于结束日期');
|
||||||
const classroom = await this.classroomRepo.findOne({ where: { id: dto.classroomId } });
|
const classroom = await this.classroomRepo.findOne({ where: { id: dto.classroomId } });
|
||||||
if (!classroom) throw new NotFoundException('教室不存在');
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
if (classroom.status !== ClassroomStatus.AVAILABLE) {
|
||||||
|
throw new BadRequestException('仅可用教室可以创建租赁');
|
||||||
|
}
|
||||||
const lessorOrganization = dto.lessorOrganizationId
|
const lessorOrganization = dto.lessorOrganizationId
|
||||||
? await this.organizationRepo.findOne({
|
? await this.organizationRepo.findOne({
|
||||||
where: { id: dto.lessorOrganizationId, status: 'active' },
|
where: { id: dto.lessorOrganizationId, status: 'active' },
|
||||||
@@ -253,7 +259,7 @@ export class ClassroomRentalsService {
|
|||||||
lessorOrganizationId: lessorOrganization.id,
|
lessorOrganizationId: lessorOrganization.id,
|
||||||
lesseeOrganizationId: lesseeOrganization.id,
|
lesseeOrganizationId: lesseeOrganization.id,
|
||||||
createdBy: userId,
|
createdBy: userId,
|
||||||
status: 'active',
|
status: ClassroomRentalStatus.ACTIVE,
|
||||||
});
|
});
|
||||||
const saved = await this.repo.save(rental);
|
const saved = await this.repo.save(rental);
|
||||||
await this.syncScheduleFromRental(saved, lesseeOrganization.name);
|
await this.syncScheduleFromRental(saved, lesseeOrganization.name);
|
||||||
@@ -262,11 +268,21 @@ export class ClassroomRentalsService {
|
|||||||
|
|
||||||
async update(id: number, dto: UpdateRentalDto) {
|
async update(id: number, dto: UpdateRentalDto) {
|
||||||
const rental = await this.findOne(id);
|
const rental = await this.findOne(id);
|
||||||
|
if (rental.effectiveStatus !== ClassroomRentalStatus.ACTIVE) {
|
||||||
|
throw new BadRequestException('已结束或已取消的租赁不能编辑');
|
||||||
|
}
|
||||||
// 若修改了教室/日期,重新冲突检查
|
// 若修改了教室/日期,重新冲突检查
|
||||||
const newClassroomId = dto.classroomId ?? rental.classroomId;
|
const newClassroomId = dto.classroomId ?? rental.classroomId;
|
||||||
const newStart = dto.startDate ?? rental.startDate;
|
const newStart = dto.startDate ?? rental.startDate;
|
||||||
const newEnd = dto.endDate ?? rental.endDate;
|
const newEnd = dto.endDate ?? rental.endDate;
|
||||||
if (newStart > newEnd) throw new BadRequestException('起始日期不能晚于结束日期');
|
if (newStart > newEnd) throw new BadRequestException('起始日期不能晚于结束日期');
|
||||||
|
if (dto.classroomId && dto.classroomId !== rental.classroomId) {
|
||||||
|
const classroom = await this.classroomRepo.findOne({ where: { id: dto.classroomId } });
|
||||||
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
if (classroom.status !== ClassroomStatus.AVAILABLE) {
|
||||||
|
throw new BadRequestException('仅可用教室可以承接租赁');
|
||||||
|
}
|
||||||
|
}
|
||||||
if (dto.classroomId || dto.startDate || dto.endDate) {
|
if (dto.classroomId || dto.startDate || dto.endDate) {
|
||||||
const conflicts = await this.findConflicts(newClassroomId, newStart, newEnd, id);
|
const conflicts = await this.findConflicts(newClassroomId, newStart, newEnd, id);
|
||||||
if (conflicts.length > 0) {
|
if (conflicts.length > 0) {
|
||||||
@@ -300,16 +316,46 @@ export class ClassroomRentalsService {
|
|||||||
}
|
}
|
||||||
await this.repo.update(id, dto);
|
await this.repo.update(id, dto);
|
||||||
const updated = await this.findOne(id);
|
const updated = await this.findOne(id);
|
||||||
if (dto.status === 'cancelled') {
|
await this.syncScheduleFromRental(updated);
|
||||||
await this.scheduleRepo.delete({ rentalId: id, scheduleType: 'RENTAL' });
|
|
||||||
} else {
|
|
||||||
await this.syncScheduleFromRental(updated);
|
|
||||||
}
|
|
||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async cancel(id: number) {
|
||||||
|
const rental = await this.findOne(id);
|
||||||
|
if (rental.effectiveStatus !== ClassroomRentalStatus.ACTIVE) {
|
||||||
|
throw new BadRequestException('仅有效租赁可以取消');
|
||||||
|
}
|
||||||
|
await this.repo.update(id, { status: ClassroomRentalStatus.CANCELLED });
|
||||||
|
await this.scheduleRepo.delete({ rentalId: id, scheduleType: 'RENTAL' });
|
||||||
|
return this.findOne(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async end(id: number) {
|
||||||
|
const rental = await this.findOne(id);
|
||||||
|
if (rental.effectiveStatus !== ClassroomRentalStatus.ACTIVE) {
|
||||||
|
throw new BadRequestException('仅有效租赁可以结束');
|
||||||
|
}
|
||||||
|
const today = new Intl.DateTimeFormat('en-CA', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
}).format(new Date());
|
||||||
|
if (rental.startDate > today) throw new BadRequestException('租赁尚未开始,不能结束');
|
||||||
|
await this.repo.update(id, {
|
||||||
|
status: ClassroomRentalStatus.ENDED,
|
||||||
|
endDate: rental.endDate > today ? today : rental.endDate,
|
||||||
|
});
|
||||||
|
const ended = await this.findOne(id);
|
||||||
|
await this.syncScheduleFromRental(ended);
|
||||||
|
return ended;
|
||||||
|
}
|
||||||
|
|
||||||
async remove(id: number) {
|
async remove(id: number) {
|
||||||
const rental = await this.findOne(id);
|
const rental = await this.findOne(id);
|
||||||
|
if (rental.effectiveStatus === ClassroomRentalStatus.ACTIVE) {
|
||||||
|
throw new BadRequestException('进行中的租赁请先取消或结束');
|
||||||
|
}
|
||||||
// 同步删除对应排课记录
|
// 同步删除对应排课记录
|
||||||
await this.scheduleRepo.delete({ rentalId: id, scheduleType: 'RENTAL' });
|
await this.scheduleRepo.delete({ rentalId: id, scheduleType: 'RENTAL' });
|
||||||
// 同时删除合同文件
|
// 同时删除合同文件
|
||||||
@@ -327,6 +373,20 @@ export class ClassroomRentalsService {
|
|||||||
return { message: '删除成功' };
|
return { message: '删除成功' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private withEffectiveStatus(rental: ClassroomRental) {
|
||||||
|
const today = new Intl.DateTimeFormat('en-CA', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
}).format(new Date());
|
||||||
|
const effectiveStatus =
|
||||||
|
rental.status === ClassroomRentalStatus.ACTIVE && rental.endDate < today
|
||||||
|
? ClassroomRentalStatus.ENDED
|
||||||
|
: rental.status;
|
||||||
|
return Object.assign(rental, { effectiveStatus });
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步租赁订单到 class_schedules(schedule_type = 'RENTAL')
|
* 同步租赁订单到 class_schedules(schedule_type = 'RENTAL')
|
||||||
*/
|
*/
|
||||||
@@ -348,7 +408,7 @@ export class ClassroomRentalsService {
|
|||||||
teacherId: null,
|
teacherId: null,
|
||||||
scheduleType: 'RENTAL',
|
scheduleType: 'RENTAL',
|
||||||
rentalId: rental.id,
|
rentalId: rental.id,
|
||||||
status: 'active',
|
status: rental.status === ClassroomRentalStatus.CANCELLED ? 'cancelled' : 'active',
|
||||||
notes: rental.notes,
|
notes: rental.notes,
|
||||||
};
|
};
|
||||||
if (schedule) {
|
if (schedule) {
|
||||||
@@ -437,14 +497,16 @@ export class ClassroomRentalsService {
|
|||||||
const last = `${year}-${String(month).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`;
|
const last = `${year}-${String(month).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`;
|
||||||
|
|
||||||
const classrooms = await this.classroomRepo.find({
|
const classrooms = await this.classroomRepo.find({
|
||||||
where: { status: Not('archived') },
|
where: { status: Not(ClassroomStatus.ARCHIVED) },
|
||||||
order: { building: 'ASC', name: 'ASC' },
|
order: { building: 'ASC', name: 'ASC' },
|
||||||
});
|
});
|
||||||
const rentals = await this.repo
|
const rentals = await this.repo
|
||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.leftJoinAndSelect('r.lesseeOrganization', 'lesseeOrganization')
|
.leftJoinAndSelect('r.lesseeOrganization', 'lesseeOrganization')
|
||||||
.leftJoinAndSelect('r.classroom', 'classroom')
|
.leftJoinAndSelect('r.classroom', 'classroom')
|
||||||
.where('r.status != :cancelled', { cancelled: 'cancelled' })
|
.where('r.status IN (:...statuses)', {
|
||||||
|
statuses: [ClassroomRentalStatus.ACTIVE, ClassroomRentalStatus.ENDED],
|
||||||
|
})
|
||||||
.andWhere('r.startDate <= :last AND r.endDate >= :first', { first, last })
|
.andWhere('r.startDate <= :last AND r.endDate >= :first', { first, last })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IsOptional, IsString, IsInt, IsNumber, IsEnum, IsDateString } from 'class-validator';
|
import { IsOptional, IsString, IsInt, IsNumber, IsDateString } from 'class-validator';
|
||||||
|
|
||||||
export class CreateRentalDto {
|
export class CreateRentalDto {
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@@ -62,8 +62,4 @@ export class UpdateRentalDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(['active', 'ended', 'cancelled'])
|
|
||||||
status?: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository, Not } from 'typeorm';
|
import { Repository, Not, MoreThanOrEqual } from 'typeorm';
|
||||||
import { Classroom } from '../entities/classroom.entity';
|
import { Classroom, ClassroomStatus } from '../entities/classroom.entity';
|
||||||
import { ClassroomRental } from '../entities/classroom-rental.entity';
|
import { ClassroomRental, ClassroomRentalStatus } from '../entities/classroom-rental.entity';
|
||||||
import { ClassSchedule } from '../entities/class-schedule.entity';
|
import { ClassSchedule } from '../entities/class-schedule.entity';
|
||||||
import { CreateClassroomDto, UpdateClassroomDto } from './dto/classroom.dto';
|
import { CreateClassroomDto, UpdateClassroomDto } from './dto/classroom.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ClassroomsService {
|
export class ClassroomsService {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Classroom) private repo: Repository<Classroom>,
|
@InjectRepository(Classroom) private repo: Repository<Classroom>,
|
||||||
@InjectRepository(ClassroomRental) private rentalRepo: Repository<ClassroomRental>,
|
@InjectRepository(ClassroomRental) private rentalRepo: Repository<ClassroomRental>,
|
||||||
@@ -21,49 +20,148 @@ export class ClassroomsService {
|
|||||||
if (query?.roomType) where.roomType = query.roomType;
|
if (query?.roomType) where.roomType = query.roomType;
|
||||||
if (!query?.includeArchived) where.status = Not('archived');
|
if (!query?.includeArchived) where.status = Not('archived');
|
||||||
const list = await this.repo.find({ where, order: { building: 'ASC', name: 'ASC' } });
|
const list = await this.repo.find({ where, order: { building: 'ASC', name: 'ASC' } });
|
||||||
const usageMap = await this.getCurrentUsageForClassrooms(list.map((c) => c.id));
|
const usageMap = await this.getUsageForClassrooms(list.map((c) => c.id));
|
||||||
return list.map((c) => ({ ...c, currentUsage: usageMap.get(c.id) ?? null }));
|
return list.map((classroom) => this.withEffectiveStatus(classroom, usageMap.get(classroom.id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: number) {
|
async findOne(id: number) {
|
||||||
const cls = await this.repo.findOne({ where: { id } });
|
const cls = await this.repo.findOne({ where: { id } });
|
||||||
if (!cls) throw new NotFoundException('教室不存在');
|
if (!cls) throw new NotFoundException('教室不存在');
|
||||||
const usageMap = await this.getCurrentUsageForClassrooms([id]);
|
const usageMap = await this.getUsageForClassrooms([id]);
|
||||||
return { ...cls, currentUsage: usageMap.get(id) ?? null };
|
return this.withEffectiveStatus(cls, usageMap.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(dto: CreateClassroomDto) {
|
async create(dto: CreateClassroomDto) {
|
||||||
const exists = await this.repo.findOne({ where: { name: dto.name } });
|
const exists = await this.repo.findOne({ where: { name: dto.name } });
|
||||||
if (exists) throw new BadRequestException(`教室 ${dto.name} 已存在`);
|
if (exists) throw new BadRequestException(`教室 ${dto.name} 已存在`);
|
||||||
return this.repo.save(this.repo.create(dto));
|
return this.repo.save(this.repo.create({ ...dto, status: ClassroomStatus.AVAILABLE }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: number, dto: UpdateClassroomDto) {
|
async update(id: number, dto: UpdateClassroomDto) {
|
||||||
await this.findOne(id);
|
const classroom = await this.repo.findOne({ where: { id } });
|
||||||
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
if (dto.status === ClassroomStatus.MAINTENANCE && classroom.status !== dto.status) {
|
||||||
|
await this.assertNoActiveAllocations(id);
|
||||||
|
}
|
||||||
await this.repo.update(id, dto);
|
await this.repo.update(id, dto);
|
||||||
return this.repo.findOne({ where: { id } });
|
return this.repo.findOne({ where: { id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: number) {
|
async remove(id: number) {
|
||||||
await this.findOne(id);
|
const classroom = await this.repo.findOne({ where: { id } });
|
||||||
await this.repo.update(id, { status: 'archived' });
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
await this.assertNoActiveAllocations(id);
|
||||||
|
await this.repo.update(id, { status: ClassroomStatus.ARCHIVED });
|
||||||
return { message: '已归档' };
|
return { message: '已归档' };
|
||||||
}
|
}
|
||||||
|
|
||||||
async restore(id: number) {
|
async restore(id: number) {
|
||||||
await this.findOne(id);
|
const classroom = await this.repo.findOne({ where: { id } });
|
||||||
await this.repo.update(id, { status: 'reserved' });
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
await this.repo.update(id, { status: ClassroomStatus.AVAILABLE });
|
||||||
return this.repo.findOne({ where: { id } });
|
return this.repo.findOne({ where: { id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCurrentUsageForClassrooms(classroomIds: number[]): Promise<Map<number, { type: 'schedule' | 'rental'; title: string; startTime: string; endTime: string }>> {
|
private withEffectiveStatus(
|
||||||
const result = new Map<number, { type: 'schedule' | 'rental'; title: string; startTime: string; endTime: string }>();
|
classroom: Classroom,
|
||||||
|
usage?: {
|
||||||
|
state: 'in_use' | 'reserved';
|
||||||
|
currentUsage: {
|
||||||
|
type: 'schedule' | 'rental';
|
||||||
|
title: string;
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
} | null;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const effectiveStatus =
|
||||||
|
classroom.status === ClassroomStatus.ARCHIVED ||
|
||||||
|
classroom.status === ClassroomStatus.MAINTENANCE
|
||||||
|
? classroom.status
|
||||||
|
: (usage?.state ?? ClassroomStatus.AVAILABLE);
|
||||||
|
return { ...classroom, currentUsage: usage?.currentUsage ?? null, effectiveStatus };
|
||||||
|
}
|
||||||
|
|
||||||
|
private async assertNoActiveAllocations(classroomId: number) {
|
||||||
|
const today = new Intl.DateTimeFormat('en-CA', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
}).format(new Date());
|
||||||
|
const scheduleCount = await this.scheduleRepo
|
||||||
|
.createQueryBuilder('schedule')
|
||||||
|
.where('schedule.classroomId = :classroomId', { classroomId })
|
||||||
|
.andWhere('schedule.status = :active', { active: 'active' })
|
||||||
|
.andWhere('schedule.endDate >= :today', { today })
|
||||||
|
.getCount();
|
||||||
|
const rentalCount = await this.rentalRepo.count({
|
||||||
|
where: {
|
||||||
|
classroomId,
|
||||||
|
status: ClassroomRentalStatus.ACTIVE,
|
||||||
|
endDate: MoreThanOrEqual(today),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (rentalCount > 0 || scheduleCount > 0) {
|
||||||
|
throw new BadRequestException('该教室存在有效排课或租赁,无法维护或归档');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getUsageForClassrooms(classroomIds: number[]): Promise<
|
||||||
|
Map<
|
||||||
|
number,
|
||||||
|
{
|
||||||
|
state: 'in_use' | 'reserved';
|
||||||
|
currentUsage: {
|
||||||
|
type: 'schedule' | 'rental';
|
||||||
|
title: string;
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
>
|
||||||
|
> {
|
||||||
|
const result = new Map<
|
||||||
|
number,
|
||||||
|
{
|
||||||
|
state: 'in_use' | 'reserved';
|
||||||
|
currentUsage: {
|
||||||
|
type: 'schedule' | 'rental';
|
||||||
|
title: string;
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
>();
|
||||||
if (classroomIds.length === 0) return result;
|
if (classroomIds.length === 0) return result;
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const todayStr = now.toISOString().slice(0, 10);
|
const todayStr = new Intl.DateTimeFormat('en-CA', {
|
||||||
const currentTime = now.toTimeString().slice(0, 5);
|
timeZone: 'Asia/Shanghai',
|
||||||
const weekDay = now.getDay() || 7;
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
}).format(now);
|
||||||
|
const currentTime = new Intl.DateTimeFormat('en-GB', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
hour12: false,
|
||||||
|
}).format(now);
|
||||||
|
const shanghaiParts = new Intl.DateTimeFormat('en-US', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
weekday: 'short',
|
||||||
|
}).format(now);
|
||||||
|
const weekDayMap: Record<string, number> = {
|
||||||
|
Mon: 1,
|
||||||
|
Tue: 2,
|
||||||
|
Wed: 3,
|
||||||
|
Thu: 4,
|
||||||
|
Fri: 5,
|
||||||
|
Sat: 6,
|
||||||
|
Sun: 7,
|
||||||
|
};
|
||||||
|
const weekDay = weekDayMap[shanghaiParts];
|
||||||
|
|
||||||
const schedules = await this.scheduleRepo
|
const schedules = await this.scheduleRepo
|
||||||
.createQueryBuilder('s')
|
.createQueryBuilder('s')
|
||||||
@@ -71,26 +169,37 @@ export class ClassroomsService {
|
|||||||
.select('s.classroomId', 'classroomId')
|
.select('s.classroomId', 'classroomId')
|
||||||
.addSelect('s.startTime', 'startTime')
|
.addSelect('s.startTime', 'startTime')
|
||||||
.addSelect('s.endTime', 'endTime')
|
.addSelect('s.endTime', 'endTime')
|
||||||
|
.addSelect('s.startDate', 'startDate')
|
||||||
|
.addSelect('s.endDate', 'endDate')
|
||||||
|
.addSelect('s.weekDay', 'weekDay')
|
||||||
.addSelect('s.subject', 'subject')
|
.addSelect('s.subject', 'subject')
|
||||||
.addSelect('c.name', 'className')
|
.addSelect('c.name', 'className')
|
||||||
.where('s.classroomId IN (:...ids)', { ids: classroomIds })
|
.where('s.classroomId IN (:...ids)', { ids: classroomIds })
|
||||||
.andWhere('s.status = :active', { active: 'active' })
|
.andWhere('s.status = :active', { active: 'active' })
|
||||||
.andWhere('s.scheduleType = :type', { type: 'INTERNAL' })
|
.andWhere('s.scheduleType = :type', { type: 'INTERNAL' })
|
||||||
.andWhere('s.startDate <= :today', { today: todayStr })
|
|
||||||
.andWhere('s.endDate >= :today', { today: todayStr })
|
.andWhere('s.endDate >= :today', { today: todayStr })
|
||||||
.andWhere('s.weekDay = :weekDay', { weekDay })
|
|
||||||
.andWhere('s.startTime <= :currentTime', { currentTime })
|
|
||||||
.andWhere('s.endTime >= :currentTime', { currentTime })
|
|
||||||
.getRawMany();
|
.getRawMany();
|
||||||
|
|
||||||
for (const s of schedules) {
|
for (const schedule of schedules) {
|
||||||
const classroomId = Number(s.classroomId);
|
const classroomId = Number(schedule.classroomId);
|
||||||
if (!result.has(classroomId)) {
|
const isCurrent =
|
||||||
|
String(schedule.startDate) <= todayStr &&
|
||||||
|
String(schedule.endDate) >= todayStr &&
|
||||||
|
Number(schedule.weekDay) === weekDay &&
|
||||||
|
String(schedule.startTime) <= currentTime &&
|
||||||
|
String(schedule.endTime) >= currentTime;
|
||||||
|
const existing = result.get(classroomId);
|
||||||
|
if (!existing || isCurrent) {
|
||||||
result.set(classroomId, {
|
result.set(classroomId, {
|
||||||
type: 'schedule',
|
state: isCurrent ? 'in_use' : 'reserved',
|
||||||
title: `${s.className || ''} ${s.subject || ''}`.trim() || '内部课程',
|
currentUsage: isCurrent
|
||||||
startTime: String(s.startTime),
|
? {
|
||||||
endTime: String(s.endTime),
|
type: 'schedule',
|
||||||
|
title: `${schedule.className || ''} ${schedule.subject || ''}`.trim() || '内部课程',
|
||||||
|
startTime: String(schedule.startTime),
|
||||||
|
endTime: String(schedule.endTime),
|
||||||
|
}
|
||||||
|
: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,19 +212,25 @@ export class ClassroomsService {
|
|||||||
.addSelect('r.endDate', 'endDate')
|
.addSelect('r.endDate', 'endDate')
|
||||||
.addSelect('t.name', 'tenantName')
|
.addSelect('t.name', 'tenantName')
|
||||||
.where('r.classroomId IN (:...ids)', { ids: classroomIds })
|
.where('r.classroomId IN (:...ids)', { ids: classroomIds })
|
||||||
.andWhere('r.status != :cancelled', { cancelled: 'cancelled' })
|
.andWhere('r.status = :active', { active: ClassroomRentalStatus.ACTIVE })
|
||||||
.andWhere('r.startDate <= :today', { today: todayStr })
|
|
||||||
.andWhere('r.endDate >= :today', { today: todayStr })
|
.andWhere('r.endDate >= :today', { today: todayStr })
|
||||||
.getRawMany();
|
.getRawMany();
|
||||||
|
|
||||||
for (const r of rentals) {
|
for (const rental of rentals) {
|
||||||
const classroomId = Number(r.classroomId);
|
const classroomId = Number(rental.classroomId);
|
||||||
if (!result.has(classroomId)) {
|
const isCurrent = String(rental.startDate) <= todayStr && String(rental.endDate) >= todayStr;
|
||||||
|
const existing = result.get(classroomId);
|
||||||
|
if (!existing || isCurrent) {
|
||||||
result.set(classroomId, {
|
result.set(classroomId, {
|
||||||
type: 'rental',
|
state: isCurrent ? 'in_use' : 'reserved',
|
||||||
title: r.tenantName ? `${r.tenantName} 租赁` : '外部租赁',
|
currentUsage: isCurrent
|
||||||
startTime: '00:00',
|
? {
|
||||||
endTime: '23:59',
|
type: 'rental',
|
||||||
|
title: rental.tenantName ? `${rental.tenantName} 租赁` : '外部租赁',
|
||||||
|
startTime: '00:00',
|
||||||
|
endTime: '23:59',
|
||||||
|
}
|
||||||
|
: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,24 +251,38 @@ export class ClassroomsService {
|
|||||||
let skipped = 0;
|
let skipped = 0;
|
||||||
const errors: string[] = [];
|
const errors: string[] = [];
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
if (!row.name?.trim()) { skipped++; continue; }
|
if (!row.name?.trim()) {
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const exists = await this.repo.findOne({ where: { name: row.name.trim() } });
|
const exists = await this.repo.findOne({ where: { name: row.name.trim() } });
|
||||||
if (exists) { errors.push(`教室 ${row.name} 已存在`); skipped++; continue; }
|
if (exists) {
|
||||||
|
errors.push(`教室 ${row.name} 已存在`);
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
await this.repo.save(this.repo.create({ ...row, capacity: row.capacity || 30 }));
|
await this.repo.save(this.repo.create({ ...row, capacity: row.capacity || 30 }));
|
||||||
imported++;
|
imported++;
|
||||||
}
|
}
|
||||||
return { message: `成功导入 ${imported} 间教室,跳过 ${skipped} 间`, imported, skipped, errors: errors.length > 0 ? errors : undefined };
|
return {
|
||||||
|
message: `成功导入 ${imported} 间教室,跳过 ${skipped} 间`,
|
||||||
|
imported,
|
||||||
|
skipped,
|
||||||
|
errors: errors.length > 0 ? errors : undefined,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUsageReport(dateFrom: string, dateTo: string) {
|
async getUsageReport(dateFrom: string, dateTo: string) {
|
||||||
const classrooms = await this.repo.find({
|
const classrooms = await this.repo.find({
|
||||||
where: { status: Not('archived') },
|
where: { status: Not(ClassroomStatus.ARCHIVED) },
|
||||||
order: { building: 'ASC', name: 'ASC' },
|
order: { building: 'ASC', name: 'ASC' },
|
||||||
});
|
});
|
||||||
|
|
||||||
const rentals = await this.rentalRepo
|
const rentals = await this.rentalRepo
|
||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.where('r.status != :cancelled', { cancelled: 'cancelled' })
|
.where('r.status IN (:...statuses)', {
|
||||||
|
statuses: [ClassroomRentalStatus.ACTIVE, ClassroomRentalStatus.ENDED],
|
||||||
|
})
|
||||||
.andWhere('r.startDate <= :dateTo AND r.endDate >= :dateFrom', { dateFrom, dateTo })
|
.andWhere('r.startDate <= :dateTo AND r.endDate >= :dateFrom', { dateFrom, dateTo })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
|
||||||
|
|||||||
39
apps/server/src/classrooms/classrooms.status.spec.ts
Normal file
39
apps/server/src/classrooms/classrooms.status.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
|
import { ClassroomStatus } from '../entities/classroom.entity';
|
||||||
|
import { ClassroomsService } from './classrooms.service';
|
||||||
|
|
||||||
|
function createService(options?: { rentals?: number; schedules?: number }) {
|
||||||
|
const repo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue({ id: 1, name: 'A101', status: ClassroomStatus.ARCHIVED }),
|
||||||
|
update: jest.fn(),
|
||||||
|
};
|
||||||
|
const rentalRepo = { count: jest.fn().mockResolvedValue(options?.rentals ?? 0) };
|
||||||
|
const scheduleRepo = {
|
||||||
|
createQueryBuilder: jest.fn().mockReturnValue({
|
||||||
|
where: jest.fn().mockReturnThis(),
|
||||||
|
andWhere: jest.fn().mockReturnThis(),
|
||||||
|
getCount: jest.fn().mockResolvedValue(options?.schedules ?? 0),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
service: new ClassroomsService(repo as never, rentalRepo as never, scheduleRepo as never),
|
||||||
|
repo,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('ClassroomsService — persisted classroom status', () => {
|
||||||
|
it('restores an archived classroom to available', async () => {
|
||||||
|
const { service, repo } = createService();
|
||||||
|
|
||||||
|
await service.restore(1);
|
||||||
|
|
||||||
|
expect(repo.update).toHaveBeenCalledWith(1, { status: ClassroomStatus.AVAILABLE });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects archiving a classroom with active allocations', async () => {
|
||||||
|
const { service, repo } = createService({ rentals: 1 });
|
||||||
|
|
||||||
|
await expect(service.remove(1)).rejects.toBeInstanceOf(BadRequestException);
|
||||||
|
expect(repo.update).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { IsOptional, IsString, IsNotEmpty, IsInt, IsEnum } from 'class-validator';
|
import { IsOptional, IsString, IsNotEmpty, IsInt, IsEnum } from 'class-validator';
|
||||||
|
import { ClassroomStatus } from '../../entities/classroom.entity';
|
||||||
|
|
||||||
export class CreateClassroomDto {
|
export class CreateClassroomDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@@ -21,11 +22,9 @@ export class CreateClassroomDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
roomType?: string; // 大 / 次大 / 小
|
roomType?: string; // 大 / 次大 / 小
|
||||||
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateClassroomDto {
|
export class UpdateClassroomDto {
|
||||||
@@ -49,12 +48,11 @@ export class UpdateClassroomDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
roomType?: string;
|
roomType?: string;
|
||||||
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsEnum(['available', 'archived'])
|
@IsEnum([ClassroomStatus.AVAILABLE, ClassroomStatus.MAINTENANCE])
|
||||||
status?: string;
|
status?: ClassroomStatus;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,13 +119,12 @@ export class DashboardService {
|
|||||||
const pendingQb = this.depositRepo
|
const pendingQb = this.depositRepo
|
||||||
.createQueryBuilder('d')
|
.createQueryBuilder('d')
|
||||||
.select('SUM(d.amount)', 'total')
|
.select('SUM(d.amount)', 'total')
|
||||||
.where('d.status = :paid', { paid: 'paid' })
|
.where('d.status = :paid', { paid: 'paid' });
|
||||||
.andWhere('d.refundStatus IS NULL');
|
|
||||||
const pendingResult = await pendingQb.getRawOne();
|
const pendingResult = await pendingQb.getRawOne();
|
||||||
const pendingDeposits = parseFloat(pendingResult?.total || '0');
|
const pendingDeposits = parseFloat(pendingResult?.total || '0');
|
||||||
|
|
||||||
const activeRentals = await this.rentalRepo.count({
|
const activeRentals = await this.rentalRepo.count({
|
||||||
where: { endDate: MoreThanOrEqual(todayStr) },
|
where: { status: 'active' as const, endDate: MoreThanOrEqual(todayStr) },
|
||||||
});
|
});
|
||||||
|
|
||||||
const occByBldQb = this.occRepo
|
const occByBldQb = this.occRepo
|
||||||
@@ -366,7 +365,7 @@ export class DashboardService {
|
|||||||
|
|
||||||
async getClassroomOccupancy() {
|
async getClassroomOccupancy() {
|
||||||
const classrooms = await this.classroomRepo.find({
|
const classrooms = await this.classroomRepo.find({
|
||||||
where: { status: Not('archived') },
|
where: { status: 'available' as const },
|
||||||
order: { building: 'ASC', name: 'ASC' },
|
order: { building: 'ASC', name: 'ASC' },
|
||||||
});
|
});
|
||||||
const today = new Date().toISOString().slice(0, 10);
|
const today = new Date().toISOString().slice(0, 10);
|
||||||
@@ -383,7 +382,7 @@ export class DashboardService {
|
|||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.select('r.classroomId', 'classroomId')
|
.select('r.classroomId', 'classroomId')
|
||||||
.addSelect('COUNT(*)', 'rentalCount')
|
.addSelect('COUNT(*)', 'rentalCount')
|
||||||
.where('r.status != :cancelled', { cancelled: 'cancelled' })
|
.where('r.status = :active', { active: 'active' })
|
||||||
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today })
|
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today })
|
||||||
.groupBy('r.classroomId');
|
.groupBy('r.classroomId');
|
||||||
const rentals = await rentalQb.getRawMany();
|
const rentals = await rentalQb.getRawMany();
|
||||||
@@ -403,7 +402,7 @@ export class DashboardService {
|
|||||||
|
|
||||||
async getClassroomUtilizationStats() {
|
async getClassroomUtilizationStats() {
|
||||||
const totalClassrooms = await this.classroomRepo.count({
|
const totalClassrooms = await this.classroomRepo.count({
|
||||||
where: { status: Not('archived') },
|
where: { status: 'available' as const },
|
||||||
});
|
});
|
||||||
|
|
||||||
const today = new Date().toISOString().slice(0, 10);
|
const today = new Date().toISOString().slice(0, 10);
|
||||||
@@ -421,7 +420,7 @@ export class DashboardService {
|
|||||||
const rentalQb = this.rentalRepo
|
const rentalQb = this.rentalRepo
|
||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.select('COUNT(DISTINCT r.classroomId)', 'cnt')
|
.select('COUNT(DISTINCT r.classroomId)', 'cnt')
|
||||||
.where('r.status != :cancelled', { cancelled: 'cancelled' })
|
.where('r.status = :active', { active: 'active' })
|
||||||
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today });
|
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today });
|
||||||
const rentalResult = await rentalQb.getRawOne();
|
const rentalResult = await rentalQb.getRawOne();
|
||||||
|
|
||||||
@@ -438,7 +437,7 @@ export class DashboardService {
|
|||||||
const combinedRentalQb = this.rentalRepo
|
const combinedRentalQb = this.rentalRepo
|
||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.select('r.classroomId')
|
.select('r.classroomId')
|
||||||
.where('r.status != :cancelled', { cancelled: 'cancelled' })
|
.where('r.status = :active', { active: 'active' })
|
||||||
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today })
|
.andWhere('r.startDate <= :today AND r.endDate >= :today', { today })
|
||||||
.groupBy('r.classroomId');
|
.groupBy('r.classroomId');
|
||||||
const rentalIds = await combinedRentalQb.getRawMany();
|
const rentalIds = await combinedRentalQb.getRawMany();
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||||
|
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||||
|
|
||||||
|
function createRunner(tableExists: boolean, columns: string[] = []) {
|
||||||
|
return {
|
||||||
|
connect: jest.fn(),
|
||||||
|
release: jest.fn(),
|
||||||
|
getTables: jest.fn().mockResolvedValue(tableExists ? [{ name: 'class_student' }] : []),
|
||||||
|
getTable: jest.fn().mockResolvedValue({
|
||||||
|
name: 'class_student',
|
||||||
|
columns: columns.map((name) => ({ name })),
|
||||||
|
}),
|
||||||
|
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createService(runner: ReturnType<typeof createRunner>) {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
DatabaseMigrationsService,
|
||||||
|
{
|
||||||
|
provide: getDataSourceToken(),
|
||||||
|
useValue: {
|
||||||
|
options: { type: 'better-sqlite3' },
|
||||||
|
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
return module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||||
|
removeUnusedClassStudentColumns(): Promise<void>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DatabaseMigrationsService — class student cleanup', () => {
|
||||||
|
it('drops the unused enrollment_id column', async () => {
|
||||||
|
const runner = createRunner(true, ['id', 'enrollment_id']);
|
||||||
|
const service = await createService(runner);
|
||||||
|
|
||||||
|
await service.removeUnusedClassStudentColumns();
|
||||||
|
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('class_student', 'enrollment_id');
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does nothing when the table is absent', async () => {
|
||||||
|
const runner = createRunner(false);
|
||||||
|
const service = await createService(runner);
|
||||||
|
|
||||||
|
await service.removeUnusedClassStudentColumns();
|
||||||
|
|
||||||
|
expect(runner.dropColumn).not.toHaveBeenCalled();
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||||
|
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||||
|
|
||||||
|
describe('DatabaseMigrationsService — classroom status normalization', () => {
|
||||||
|
it('normalizes legacy persisted statuses to available', async () => {
|
||||||
|
const runner = {
|
||||||
|
connect: jest.fn(),
|
||||||
|
release: jest.fn(),
|
||||||
|
getTables: jest.fn().mockResolvedValue([{ name: 'classrooms' }]),
|
||||||
|
query: jest.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
DatabaseMigrationsService,
|
||||||
|
{
|
||||||
|
provide: getDataSourceToken(),
|
||||||
|
useValue: {
|
||||||
|
options: { type: 'better-sqlite3' },
|
||||||
|
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
const service = module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||||
|
normalizeClassroomStatuses(): Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
await service.normalizeClassroomStatuses();
|
||||||
|
|
||||||
|
expect(runner.query).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("status NOT IN ('available', 'maintenance', 'archived')"),
|
||||||
|
);
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||||
|
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||||
|
|
||||||
|
function createRunner(columns: string[]) {
|
||||||
|
return {
|
||||||
|
connect: jest.fn(),
|
||||||
|
release: jest.fn(),
|
||||||
|
query: jest.fn().mockResolvedValue([]),
|
||||||
|
getTables: jest.fn().mockResolvedValue(columns.length ? [{ name: 'deposits' }] : []),
|
||||||
|
getTable: jest.fn().mockResolvedValue({
|
||||||
|
name: 'deposits',
|
||||||
|
columns: columns.map((name) => ({ name })),
|
||||||
|
}),
|
||||||
|
renameColumn: jest.fn().mockResolvedValue(undefined),
|
||||||
|
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createService(runner: ReturnType<typeof createRunner>) {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
DatabaseMigrationsService,
|
||||||
|
{
|
||||||
|
provide: getDataSourceToken(),
|
||||||
|
useValue: {
|
||||||
|
options: { type: 'better-sqlite3' },
|
||||||
|
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
return module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||||
|
cleanupDepositRefundColumns(): Promise<void>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DatabaseMigrationsService — deposit refund cleanup', () => {
|
||||||
|
it('renames refund audit fields and drops approval-flow remnants', async () => {
|
||||||
|
const runner = createRunner([
|
||||||
|
'id',
|
||||||
|
'refund_status',
|
||||||
|
'refund_requested_at',
|
||||||
|
'refund_approved_by',
|
||||||
|
'refund_approved_at',
|
||||||
|
'refund_rejected_reason',
|
||||||
|
]);
|
||||||
|
const service = await createService(runner);
|
||||||
|
|
||||||
|
await service.cleanupDepositRefundColumns();
|
||||||
|
|
||||||
|
expect(runner.renameColumn).toHaveBeenCalledWith(
|
||||||
|
'deposits',
|
||||||
|
'refund_approved_by',
|
||||||
|
'refunded_by',
|
||||||
|
);
|
||||||
|
expect(runner.renameColumn).toHaveBeenCalledWith(
|
||||||
|
'deposits',
|
||||||
|
'refund_approved_at',
|
||||||
|
'refunded_at',
|
||||||
|
);
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_status');
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_requested_at');
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_rejected_reason');
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('merges legacy audit values before dropping duplicate legacy columns', async () => {
|
||||||
|
const runner = createRunner([
|
||||||
|
'id',
|
||||||
|
'refund_approved_by',
|
||||||
|
'refund_approved_at',
|
||||||
|
'refunded_by',
|
||||||
|
'refunded_at',
|
||||||
|
]);
|
||||||
|
const service = await createService(runner);
|
||||||
|
|
||||||
|
await service.cleanupDepositRefundColumns();
|
||||||
|
|
||||||
|
expect(runner.query).toHaveBeenCalledWith(
|
||||||
|
'UPDATE deposits SET refunded_by = COALESCE(refunded_by, refund_approved_by)',
|
||||||
|
);
|
||||||
|
expect(runner.query).toHaveBeenCalledWith(
|
||||||
|
'UPDATE deposits SET refunded_at = COALESCE(refunded_at, refund_approved_at)',
|
||||||
|
);
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_approved_by');
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_approved_at');
|
||||||
|
expect(runner.renameColumn).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does nothing when the deposits table is absent', async () => {
|
||||||
|
const runner = createRunner([]);
|
||||||
|
const service = await createService(runner);
|
||||||
|
|
||||||
|
await service.cleanupDepositRefundColumns();
|
||||||
|
|
||||||
|
expect(runner.renameColumn).not.toHaveBeenCalled();
|
||||||
|
expect(runner.dropColumn).not.toHaveBeenCalled();
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||||
|
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||||
|
|
||||||
|
describe('DatabaseMigrationsService — room gender cleanup', () => {
|
||||||
|
it('drops the retired rooms.gender column', async () => {
|
||||||
|
const runner = {
|
||||||
|
connect: jest.fn(),
|
||||||
|
release: jest.fn(),
|
||||||
|
getTables: jest.fn().mockResolvedValue([{ name: 'rooms' }]),
|
||||||
|
getTable: jest.fn().mockResolvedValue({
|
||||||
|
name: 'rooms',
|
||||||
|
columns: [{ name: 'id' }, { name: 'gender' }],
|
||||||
|
}),
|
||||||
|
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||||
|
};
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
DatabaseMigrationsService,
|
||||||
|
{
|
||||||
|
provide: getDataSourceToken(),
|
||||||
|
useValue: {
|
||||||
|
options: { type: 'better-sqlite3' },
|
||||||
|
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
const service = module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||||
|
removeUnusedRoomColumns(): Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
await service.removeUnusedRoomColumns();
|
||||||
|
|
||||||
|
expect(runner.dropColumn).toHaveBeenCalledWith('rooms', 'gender');
|
||||||
|
expect(runner.release).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -15,6 +15,10 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
await this.normalizeClassDates();
|
await this.normalizeClassDates();
|
||||||
await this.protectAttendanceHistory();
|
await this.protectAttendanceHistory();
|
||||||
await this.removeUnusedClassroomColumns();
|
await this.removeUnusedClassroomColumns();
|
||||||
|
await this.removeUnusedRoomColumns();
|
||||||
|
await this.cleanupDepositRefundColumns();
|
||||||
|
await this.removeUnusedClassStudentColumns();
|
||||||
|
await this.normalizeClassroomStatuses();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async removeUnusedClassroomColumns(): Promise<void> {
|
private async removeUnusedClassroomColumns(): Promise<void> {
|
||||||
@@ -36,6 +40,92 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async removeUnusedRoomColumns(): Promise<void> {
|
||||||
|
const runner = this.dataSource.createQueryRunner();
|
||||||
|
await runner.connect();
|
||||||
|
try {
|
||||||
|
const tables = await runner.getTables(['rooms']);
|
||||||
|
if (tables.length === 0) return;
|
||||||
|
|
||||||
|
const table = await runner.getTable('rooms');
|
||||||
|
if (table?.columns.some((column) => column.name === 'gender')) {
|
||||||
|
await runner.dropColumn('rooms', 'gender');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await runner.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async cleanupDepositRefundColumns(): Promise<void> {
|
||||||
|
const runner = this.dataSource.createQueryRunner();
|
||||||
|
await runner.connect();
|
||||||
|
try {
|
||||||
|
const tables = await runner.getTables(['deposits']);
|
||||||
|
if (tables.length === 0) return;
|
||||||
|
|
||||||
|
const table = await runner.getTable('deposits');
|
||||||
|
const columnNames = new Set(table?.columns.map((column) => column.name) ?? []);
|
||||||
|
for (const [legacyName, currentName] of [
|
||||||
|
['refund_approved_by', 'refunded_by'],
|
||||||
|
['refund_approved_at', 'refunded_at'],
|
||||||
|
] as const) {
|
||||||
|
if (!columnNames.has(legacyName)) continue;
|
||||||
|
|
||||||
|
if (columnNames.has(currentName)) {
|
||||||
|
await runner.query(
|
||||||
|
`UPDATE deposits SET ${currentName} = COALESCE(${currentName}, ${legacyName})`,
|
||||||
|
);
|
||||||
|
await runner.dropColumn('deposits', legacyName);
|
||||||
|
} else {
|
||||||
|
await runner.renameColumn('deposits', legacyName, currentName);
|
||||||
|
columnNames.add(currentName);
|
||||||
|
}
|
||||||
|
columnNames.delete(legacyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const columnName of ['refund_status', 'refund_requested_at', 'refund_rejected_reason']) {
|
||||||
|
if (columnNames.has(columnName)) {
|
||||||
|
await runner.dropColumn('deposits', columnName);
|
||||||
|
columnNames.delete(columnName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await runner.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async removeUnusedClassStudentColumns(): Promise<void> {
|
||||||
|
const runner = this.dataSource.createQueryRunner();
|
||||||
|
await runner.connect();
|
||||||
|
try {
|
||||||
|
const tables = await runner.getTables(['class_student']);
|
||||||
|
if (tables.length === 0) return;
|
||||||
|
|
||||||
|
const table = await runner.getTable('class_student');
|
||||||
|
if (table?.columns.some((column) => column.name === 'enrollment_id')) {
|
||||||
|
await runner.dropColumn('class_student', 'enrollment_id');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await runner.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async normalizeClassroomStatuses(): Promise<void> {
|
||||||
|
const runner = this.dataSource.createQueryRunner();
|
||||||
|
await runner.connect();
|
||||||
|
try {
|
||||||
|
const tables = await runner.getTables(['classrooms']);
|
||||||
|
if (tables.length === 0) return;
|
||||||
|
await runner.query(`
|
||||||
|
UPDATE classrooms
|
||||||
|
SET status = 'available'
|
||||||
|
WHERE status IS NULL OR status NOT IN ('available', 'maintenance', 'archived')
|
||||||
|
`);
|
||||||
|
} finally {
|
||||||
|
await runner.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async ensureAiConfigTable(): Promise<void> {
|
private async ensureAiConfigTable(): Promise<void> {
|
||||||
const runner = this.dataSource.createQueryRunner();
|
const runner = this.dataSource.createQueryRunner();
|
||||||
await runner.connect();
|
await runner.connect();
|
||||||
@@ -354,14 +444,17 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
// Drop any existing FK constraint on schedule_id or class_id
|
// Drop any existing FK constraint on schedule_id or class_id
|
||||||
const fkColumns = ['schedule_id', 'class_id'];
|
const fkColumns = ['schedule_id', 'class_id'];
|
||||||
for (const col of fkColumns) {
|
for (const col of fkColumns) {
|
||||||
const fkRows: { CONSTRAINT_NAME: string }[] = await runner.query(`
|
const fkRows: { CONSTRAINT_NAME: string }[] = await runner.query(
|
||||||
|
`
|
||||||
SELECT CONSTRAINT_NAME
|
SELECT CONSTRAINT_NAME
|
||||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||||
WHERE TABLE_SCHEMA = DATABASE()
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
AND TABLE_NAME = 'attendance_sessions'
|
AND TABLE_NAME = 'attendance_sessions'
|
||||||
AND COLUMN_NAME = ?
|
AND COLUMN_NAME = ?
|
||||||
AND REFERENCED_TABLE_NAME IS NOT NULL
|
AND REFERENCED_TABLE_NAME IS NOT NULL
|
||||||
`, [col]);
|
`,
|
||||||
|
[col],
|
||||||
|
);
|
||||||
|
|
||||||
for (const row of fkRows) {
|
for (const row of fkRows) {
|
||||||
try {
|
try {
|
||||||
@@ -381,13 +474,16 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
];
|
];
|
||||||
for (const c of constraints) {
|
for (const c of constraints) {
|
||||||
// Only skip if RESTRICT constraint is already confirmed via information_schema
|
// Only skip if RESTRICT constraint is already confirmed via information_schema
|
||||||
const existing: Array<{ DELETE_RULE: string }> = await runner.query(`
|
const existing: Array<{ DELETE_RULE: string }> = await runner.query(
|
||||||
|
`
|
||||||
SELECT DELETE_RULE
|
SELECT DELETE_RULE
|
||||||
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
|
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
|
||||||
WHERE CONSTRAINT_SCHEMA = DATABASE()
|
WHERE CONSTRAINT_SCHEMA = DATABASE()
|
||||||
AND TABLE_NAME = 'attendance_sessions'
|
AND TABLE_NAME = 'attendance_sessions'
|
||||||
AND CONSTRAINT_NAME = ?
|
AND CONSTRAINT_NAME = ?
|
||||||
`, [c.name]);
|
`,
|
||||||
|
[c.name],
|
||||||
|
);
|
||||||
|
|
||||||
if (existing.length > 0 && existing[0].DELETE_RULE === 'RESTRICT') {
|
if (existing.length > 0 && existing[0].DELETE_RULE === 'RESTRICT') {
|
||||||
this.logger.log(`考勤场次删除保护约束已存在: ${c.name}`);
|
this.logger.log(`考勤场次删除保护约束已存在: ${c.name}`);
|
||||||
@@ -449,17 +545,13 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
FROM attendance_sessions
|
FROM attendance_sessions
|
||||||
`);
|
`);
|
||||||
await runner.query('DROP TABLE attendance_sessions');
|
await runner.query('DROP TABLE attendance_sessions');
|
||||||
await runner.query(
|
await runner.query('ALTER TABLE attendance_sessions_new RENAME TO attendance_sessions');
|
||||||
'ALTER TABLE attendance_sessions_new RENAME TO attendance_sessions',
|
|
||||||
);
|
|
||||||
await runner.query(
|
await runner.query(
|
||||||
'CREATE UNIQUE INDEX IF NOT EXISTS uq_attendance_session_schedule_date ON attendance_sessions(schedule_id, lesson_date)',
|
'CREATE UNIQUE INDEX IF NOT EXISTS uq_attendance_session_schedule_date ON attendance_sessions(schedule_id, lesson_date)',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Rebuild attendance_records to add/protect FK on attendance_session_id
|
// Rebuild attendance_records to add/protect FK on attendance_session_id
|
||||||
const recordsFk = await runner.query(
|
const recordsFk = await runner.query("PRAGMA foreign_key_list('attendance_records')");
|
||||||
"PRAGMA foreign_key_list('attendance_records')",
|
|
||||||
);
|
|
||||||
const hasSessionFk = recordsFk.some(
|
const hasSessionFk = recordsFk.some(
|
||||||
(r: { from: string }) => r.from === 'attendance_session_id',
|
(r: { from: string }) => r.from === 'attendance_session_id',
|
||||||
);
|
);
|
||||||
@@ -492,9 +584,7 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
FROM attendance_records
|
FROM attendance_records
|
||||||
`);
|
`);
|
||||||
await runner.query('DROP TABLE attendance_records');
|
await runner.query('DROP TABLE attendance_records');
|
||||||
await runner.query(
|
await runner.query('ALTER TABLE attendance_records_new RENAME TO attendance_records');
|
||||||
'ALTER TABLE attendance_records_new RENAME TO attendance_records',
|
|
||||||
);
|
|
||||||
await runner.query(
|
await runner.query(
|
||||||
'CREATE UNIQUE INDEX IF NOT EXISTS uq_attendance_session_student ON attendance_records(attendance_session_id, student_id)',
|
'CREATE UNIQUE INDEX IF NOT EXISTS uq_attendance_session_student ON attendance_records(attendance_session_id, student_id)',
|
||||||
);
|
);
|
||||||
@@ -504,9 +594,7 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
|||||||
// If violations exist, the transaction rolls back and old tables are preserved.
|
// If violations exist, the transaction rolls back and old tables are preserved.
|
||||||
const checkRows = await runner.query('PRAGMA foreign_key_check');
|
const checkRows = await runner.query('PRAGMA foreign_key_check');
|
||||||
if (checkRows.length > 0) {
|
if (checkRows.length > 0) {
|
||||||
throw new Error(
|
throw new Error(`外键一致性检查失败: ${checkRows.length} 行违反外键约束`);
|
||||||
`外键一致性检查失败: ${checkRows.length} 行违反外键约束`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await runner.query('COMMIT');
|
await runner.query('COMMIT');
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import { Student } from '../entities/student.entity';
|
|||||||
import { DepositsService } from './deposits.service';
|
import { DepositsService } from './deposits.service';
|
||||||
import { NotificationsService } from '../notifications/notifications.service';
|
import { NotificationsService } from '../notifications/notifications.service';
|
||||||
import { NotificationType } from '../entities/notification.entity';
|
import { NotificationType } from '../entities/notification.entity';
|
||||||
import { CreateDepositDto, RefundDepositDto, CreateDepositWithInstallmentsDto } from './dto/deposit.dto';
|
import { CreateDepositDto, RefundDepositDto } from './dto/deposit.dto';
|
||||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||||
import { OperationLogsService } from '../operation-logs/operation-logs.service';
|
import { OperationLogsService } from '../operation-logs/operation-logs.service';
|
||||||
import { extractRequestInfo } from '../common/request-utils';
|
import { extractRequestInfo } from '../common/request-utils';
|
||||||
@@ -61,7 +61,7 @@ export class DepositsController {
|
|||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@RequirePermission('deposit:create')
|
@RequirePermission('deposit:create')
|
||||||
async create(@Body() dto: CreateDepositDto | CreateDepositWithInstallmentsDto, @Request() req: any) {
|
async create(@Body() dto: CreateDepositDto, @Request() req: any) {
|
||||||
const { ipAddress, userAgent } = extractRequestInfo(req);
|
const { ipAddress, userAgent } = extractRequestInfo(req);
|
||||||
const result = await this.service.create(dto, req.user?.id);
|
const result = await this.service.create(dto, req.user?.id);
|
||||||
await this.logService.log({
|
await this.logService.log({
|
||||||
|
|||||||
38
apps/server/src/deposits/deposits.refund.spec.ts
Normal file
38
apps/server/src/deposits/deposits.refund.spec.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import { DepositsService } from './deposits.service';
|
||||||
|
import { Deposit } from '../entities/deposit.entity';
|
||||||
|
|
||||||
|
describe('DepositsService — direct refund', () => {
|
||||||
|
it('stores the refund result on the main status and renamed audit fields', async () => {
|
||||||
|
const deposit = {
|
||||||
|
id: 1,
|
||||||
|
amount: 500,
|
||||||
|
status: 'paid',
|
||||||
|
} as Deposit;
|
||||||
|
const repo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue(deposit),
|
||||||
|
save: jest.fn().mockImplementation(async (value: Deposit) => value),
|
||||||
|
};
|
||||||
|
const service = new DepositsService(repo as never, {} as never, {} as never);
|
||||||
|
|
||||||
|
const result = await service.refund(
|
||||||
|
1,
|
||||||
|
{
|
||||||
|
refundDate: '2026-07-13',
|
||||||
|
deductionAmount: 100,
|
||||||
|
deductionReason: '物品损坏',
|
||||||
|
},
|
||||||
|
42,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toMatchObject({
|
||||||
|
refundDate: '2026-07-13',
|
||||||
|
refundAmount: 400,
|
||||||
|
deductionAmount: 100,
|
||||||
|
deductionReason: '物品损坏',
|
||||||
|
status: 'partial_refund',
|
||||||
|
refundedBy: 42,
|
||||||
|
});
|
||||||
|
expect(result.refundedAt).toBeInstanceOf(Date);
|
||||||
|
expect(repo.save).toHaveBeenCalledWith(deposit);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,7 @@ import { Deposit } from '../entities/deposit.entity';
|
|||||||
import { Student } from '../entities/student.entity';
|
import { Student } from '../entities/student.entity';
|
||||||
import { DepositInstallment } from '../entities/deposit-installment.entity';
|
import { DepositInstallment } from '../entities/deposit-installment.entity';
|
||||||
|
|
||||||
import { CreateDepositDto, RefundDepositDto, CreateDepositWithInstallmentsDto } from './dto/deposit.dto';
|
import { CreateDepositDto, RefundDepositDto } from './dto/deposit.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DepositsService {
|
export class DepositsService {
|
||||||
@@ -55,17 +55,6 @@ export class DepositsService {
|
|||||||
recordedBy: userId,
|
recordedBy: userId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (dto instanceof CreateDepositWithInstallmentsDto && dto.installments?.length) {
|
|
||||||
deposit.installments = dto.installments.map((i) => {
|
|
||||||
const inst = this.installmentRepo.create({
|
|
||||||
amount: i.amount,
|
|
||||||
dueDate: i.dueDate,
|
|
||||||
status: 'pending',
|
|
||||||
});
|
|
||||||
return inst;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.repo.save(deposit);
|
return this.repo.save(deposit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,9 +103,8 @@ export class DepositsService {
|
|||||||
deposit.status =
|
deposit.status =
|
||||||
deduction > 0 ? (refundAmount > 0 ? 'partial_refund' : 'deducted') : 'refunded';
|
deduction > 0 ? (refundAmount > 0 ? 'partial_refund' : 'deducted') : 'refunded';
|
||||||
if (dto.notes) deposit.notes = dto.notes;
|
if (dto.notes) deposit.notes = dto.notes;
|
||||||
deposit.refundStatus = 'refunded';
|
deposit.refundedBy = userId ?? null;
|
||||||
deposit.refundApprovedBy = userId ?? null as unknown as number;
|
deposit.refundedAt = new Date();
|
||||||
deposit.refundApprovedAt = new Date();
|
|
||||||
|
|
||||||
return this.repo.save(deposit);
|
return this.repo.save(deposit);
|
||||||
}
|
}
|
||||||
|
|||||||
19
apps/server/src/deposits/dto/deposit.dto.spec.ts
Normal file
19
apps/server/src/deposits/dto/deposit.dto.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { validate } from 'class-validator';
|
||||||
|
import { CreateDepositDto } from './deposit.dto';
|
||||||
|
|
||||||
|
describe('CreateDepositDto boundaries', () => {
|
||||||
|
it('removes inline installments because they are managed after deposit creation', async () => {
|
||||||
|
const dto = Object.assign(new CreateDepositDto(), {
|
||||||
|
studentId: 1,
|
||||||
|
amount: 500,
|
||||||
|
paidDate: '2026-07-13',
|
||||||
|
installments: [{ amount: 250, dueDate: '2026-08-01' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await validate(dto, { whitelist: true });
|
||||||
|
|
||||||
|
expect(dto).toMatchObject({ studentId: 1, amount: 500, paidDate: '2026-07-13' });
|
||||||
|
expect(dto).not.toHaveProperty('installments');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { IsInt, IsNumber, IsString, IsOptional, ValidateNested } from 'class-validator';
|
import { IsInt, IsNumber, IsString, IsOptional } from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
|
||||||
|
|
||||||
export class CreateDepositDto {
|
export class CreateDepositDto {
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@@ -16,16 +15,6 @@ export class CreateDepositDto {
|
|||||||
notes?: string;
|
notes?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateInstallmentDto {
|
|
||||||
@IsNumber()
|
|
||||||
amount: number;
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
dueDate: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class RefundDepositDto {
|
export class RefundDepositDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
refundDate: string;
|
refundDate: string;
|
||||||
@@ -42,9 +31,3 @@ export class RefundDepositDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
notes?: string;
|
notes?: string;
|
||||||
}
|
}
|
||||||
export class CreateDepositWithInstallmentsDto extends CreateDepositDto {
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => CreateInstallmentDto)
|
|
||||||
installments?: CreateInstallmentDto[];
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -30,14 +30,11 @@ export class ClassStudent {
|
|||||||
@JoinColumn({ name: 'student_id' })
|
@JoinColumn({ name: 'student_id' })
|
||||||
student: Student;
|
student: Student;
|
||||||
|
|
||||||
@Column({ name: 'enrollment_id', type: 'integer', nullable: true })
|
|
||||||
enrollmentId: number;
|
|
||||||
|
|
||||||
@Column({ name: 'join_date', type: 'date', nullable: true })
|
@Column({ name: 'join_date', type: 'date', nullable: true })
|
||||||
joinDate: string;
|
joinDate: string | null;
|
||||||
|
|
||||||
@Column({ name: 'leave_date', type: 'date', nullable: true })
|
@Column({ name: 'leave_date', type: 'date', nullable: true })
|
||||||
leaveDate: string;
|
leaveDate: string | null;
|
||||||
|
|
||||||
@Column({ name: 'status', length: 10, default: 'active' })
|
@Column({ name: 'status', length: 10, default: 'active' })
|
||||||
status: string;
|
status: string;
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ import {
|
|||||||
import { Classroom } from './classroom.entity';
|
import { Classroom } from './classroom.entity';
|
||||||
import { Organization } from './organization.entity';
|
import { Organization } from './organization.entity';
|
||||||
|
|
||||||
|
export enum ClassroomRentalStatus {
|
||||||
|
ACTIVE = 'active',
|
||||||
|
ENDED = 'ended',
|
||||||
|
CANCELLED = 'cancelled',
|
||||||
|
}
|
||||||
|
|
||||||
@Entity('classroom_rentals')
|
@Entity('classroom_rentals')
|
||||||
@Index(['classroomId', 'startDate', 'endDate'])
|
@Index(['classroomId', 'startDate', 'endDate'])
|
||||||
export class ClassroomRental {
|
export class ClassroomRental {
|
||||||
@@ -57,8 +63,8 @@ export class ClassroomRental {
|
|||||||
@Column({ name: 'total_amount', type: 'decimal', precision: 10, scale: 2, nullable: true })
|
@Column({ name: 'total_amount', type: 'decimal', precision: 10, scale: 2, nullable: true })
|
||||||
totalAmount: number;
|
totalAmount: number;
|
||||||
|
|
||||||
@Column({ type: 'varchar', length: 20, default: 'active' })
|
@Column({ type: 'varchar', length: 20, default: ClassroomRentalStatus.ACTIVE })
|
||||||
status: string; // active / ended / cancelled
|
status: ClassroomRentalStatus | 'active' | 'ended' | 'cancelled';
|
||||||
|
|
||||||
@Column({ type: 'text', nullable: true })
|
@Column({ type: 'text', nullable: true })
|
||||||
notes: string;
|
notes: string;
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
|
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm';
|
||||||
|
|
||||||
|
export enum ClassroomStatus {
|
||||||
|
AVAILABLE = 'available',
|
||||||
|
MAINTENANCE = 'maintenance',
|
||||||
|
ARCHIVED = 'archived',
|
||||||
|
}
|
||||||
|
|
||||||
@Entity('classrooms')
|
@Entity('classrooms')
|
||||||
export class Classroom {
|
export class Classroom {
|
||||||
@@ -20,14 +26,12 @@ export class Classroom {
|
|||||||
@Column({ name: 'room_type', type: 'varchar', length: 20, default: '大' })
|
@Column({ name: 'room_type', type: 'varchar', length: 20, default: '大' })
|
||||||
roomType: string; // 大 / 次大 / 小
|
roomType: string; // 大 / 次大 / 小
|
||||||
|
|
||||||
|
@Column({ type: 'varchar', length: 20, default: ClassroomStatus.AVAILABLE })
|
||||||
@Column({ type: 'varchar', length: 20, default: 'reserved' })
|
status: ClassroomStatus | 'available' | 'maintenance' | 'archived';
|
||||||
status: string;
|
|
||||||
|
|
||||||
@Column({ type: 'text', nullable: true })
|
@Column({ type: 'text', nullable: true })
|
||||||
notes: string;
|
notes: string;
|
||||||
|
|
||||||
@CreateDateColumn({ name: 'created_at' })
|
@CreateDateColumn({ name: 'created_at' })
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,20 +46,11 @@ export class Deposit {
|
|||||||
@Column({ name: 'recorded_by', nullable: true })
|
@Column({ name: 'recorded_by', nullable: true })
|
||||||
recordedBy: number;
|
recordedBy: number;
|
||||||
|
|
||||||
@Column({ name: 'refund_status', length: 30, nullable: true })
|
@Column({ name: 'refunded_by', type: 'integer', nullable: true })
|
||||||
refundStatus: string; // pending | head_teacher_approved | finance_approved | refunded
|
refundedBy: number | null;
|
||||||
|
|
||||||
@Column({ name: 'refund_requested_at', type: 'datetime', nullable: true })
|
@Column({ name: 'refunded_at', type: 'datetime', nullable: true })
|
||||||
refundRequestedAt: Date;
|
refundedAt: Date | null;
|
||||||
|
|
||||||
@Column({ name: 'refund_approved_by', type: 'integer', nullable: true })
|
|
||||||
refundApprovedBy: number;
|
|
||||||
|
|
||||||
@Column({ name: 'refund_approved_at', type: 'datetime', nullable: true })
|
|
||||||
refundApprovedAt: Date;
|
|
||||||
|
|
||||||
@Column({ name: 'refund_rejected_reason', length: 500, nullable: true })
|
|
||||||
refundRejectedReason: string;
|
|
||||||
|
|
||||||
@OneToMany(() => DepositInstallment, (i) => i.deposit, { cascade: true, eager: true })
|
@OneToMany(() => DepositInstallment, (i) => i.deposit, { cascade: true, eager: true })
|
||||||
installments: DepositInstallment[];
|
installments: DepositInstallment[];
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ export { User } from './user.entity';
|
|||||||
export { OperationLog } from './operation-log.entity';
|
export { OperationLog } from './operation-log.entity';
|
||||||
export { Deposit } from './deposit.entity';
|
export { Deposit } from './deposit.entity';
|
||||||
export { DepositInstallment } from './deposit-installment.entity';
|
export { DepositInstallment } from './deposit-installment.entity';
|
||||||
export { Classroom } from './classroom.entity';
|
export { Classroom, ClassroomStatus } from './classroom.entity';
|
||||||
export { Organization } from './organization.entity';
|
export { Organization } from './organization.entity';
|
||||||
export { ClassroomRental } from './classroom-rental.entity';
|
export { ClassroomRental, ClassroomRentalStatus } from './classroom-rental.entity';
|
||||||
export { Permission } from './permission.entity';
|
export { Permission } from './permission.entity';
|
||||||
export { Role } from './role.entity';
|
export { Role } from './role.entity';
|
||||||
export { Class, ClassType, ClassStatus } from './class.entity';
|
export { Class, ClassType, ClassStatus } from './class.entity';
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
|
import {
|
||||||
|
Entity,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
Column,
|
||||||
|
CreateDateColumn,
|
||||||
|
OneToMany,
|
||||||
|
ManyToOne,
|
||||||
|
JoinColumn,
|
||||||
|
} from 'typeorm';
|
||||||
import { Occupancy } from './occupancy.entity';
|
import { Occupancy } from './occupancy.entity';
|
||||||
import { RoomExpense } from './room-expense.entity';
|
import { RoomExpense } from './room-expense.entity';
|
||||||
|
|
||||||
@@ -25,9 +33,6 @@ export class Room {
|
|||||||
@Column({ name: 'room_type', length: 20, nullable: true })
|
@Column({ name: 'room_type', length: 20, nullable: true })
|
||||||
roomType: string;
|
roomType: string;
|
||||||
|
|
||||||
@Column({ length: 10, nullable: true })
|
|
||||||
gender: string;
|
|
||||||
|
|
||||||
@Column({ name: 'rental_category', length: 10, default: 'short' })
|
@Column({ name: 'rental_category', length: 10, default: 'short' })
|
||||||
rentalCategory: string;
|
rentalCategory: string;
|
||||||
|
|
||||||
@@ -42,5 +47,4 @@ export class Room {
|
|||||||
|
|
||||||
@OneToMany(() => RoomExpense, (e) => e.room)
|
@OneToMany(() => RoomExpense, (e) => e.room)
|
||||||
roomExpenses: RoomExpense[];
|
roomExpenses: RoomExpense[];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,6 @@ export class StudentProfile {
|
|||||||
@Column({ length: 20, nullable: true })
|
@Column({ length: 20, nullable: true })
|
||||||
grade: string;
|
grade: string;
|
||||||
|
|
||||||
@Column({ name: 'campus_location', length: 100, nullable: true })
|
|
||||||
campusLocation: string;
|
|
||||||
|
|
||||||
@Column({ name: 'profile_date', type: 'date', nullable: true })
|
@Column({ name: 'profile_date', type: 'date', nullable: true })
|
||||||
profileDate: string;
|
profileDate: string;
|
||||||
|
|
||||||
|
|||||||
48
apps/server/src/integration/config/dto/config.dto.spec.ts
Normal file
48
apps/server/src/integration/config/dto/config.dto.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
|
import {
|
||||||
|
DingTalkThirdConfigDto,
|
||||||
|
IntegrationType,
|
||||||
|
SaveIntegrationConfigDto,
|
||||||
|
WeComThirdConfigDto,
|
||||||
|
} from './config.dto';
|
||||||
|
|
||||||
|
const pipe = new ValidationPipe({ transform: true, whitelist: true });
|
||||||
|
|
||||||
|
const transform = (value: unknown) =>
|
||||||
|
pipe.transform(value, { type: 'body', metatype: SaveIntegrationConfigDto });
|
||||||
|
|
||||||
|
describe('integration config request DTO', () => {
|
||||||
|
it('validates and transforms DingTalk configuration', async () => {
|
||||||
|
const result = await transform({
|
||||||
|
type: 'DINGTALK',
|
||||||
|
config: {
|
||||||
|
agentId: 'app-key',
|
||||||
|
corpId: 'corp-id',
|
||||||
|
appSecret: '',
|
||||||
|
appId: 'app-id',
|
||||||
|
ignored: 'value',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.type).toBe(IntegrationType.DINGTALK);
|
||||||
|
expect(result.config).toBeInstanceOf(DingTalkThirdConfigDto);
|
||||||
|
expect(result.config).toMatchObject({ agentId: 'app-key', corpId: 'corp-id', appSecret: '' });
|
||||||
|
expect(result.config).not.toHaveProperty('ignored');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the WeCom nested DTO and removes DingTalk-only fields', async () => {
|
||||||
|
const result = await transform({
|
||||||
|
type: 'WECOM',
|
||||||
|
config: { agentId: 'agent', corpId: 'corp', appId: 'not-supported' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.config).toBeInstanceOf(WeComThirdConfigDto);
|
||||||
|
expect(result.config).not.toHaveProperty('appId');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects invalid platform types and incomplete nested config', async () => {
|
||||||
|
await expect(transform({ type: 'UNKNOWN', config: {} })).rejects.toThrow();
|
||||||
|
await expect(transform({ type: 'DINGTALK', config: { corpId: 'corp' } })).rejects.toThrow();
|
||||||
|
await expect(transform({ type: 'DINGTALK' })).rejects.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,29 +1,71 @@
|
|||||||
/** 钉钉配置 */
|
import { Type } from 'class-transformer';
|
||||||
export interface DingTalkThirdConfig {
|
import {
|
||||||
agentId: string; // AppKey
|
IsDefined,
|
||||||
appSecret: string; // AppSecret
|
IsEnum,
|
||||||
corpId: string; // CorpId
|
IsNotEmpty,
|
||||||
startEnable: boolean; // 是否启用同步
|
IsOptional,
|
||||||
appId?: string; // 内部应用ID,用于消息推送(可选)
|
IsString,
|
||||||
|
ValidateNested,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
export enum IntegrationType {
|
||||||
|
WECOM = 'WECOM',
|
||||||
|
DINGTALK = 'DINGTALK',
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 企微配置 */
|
export class DingTalkThirdConfigDto {
|
||||||
export interface WeComThirdConfig {
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
agentId: string;
|
agentId: string;
|
||||||
appSecret: string;
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
appSecret?: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
corpId: string;
|
corpId: string;
|
||||||
startEnable: boolean;
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
appId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class WeComThirdConfigDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
agentId: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
appSecret?: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
corpId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class IntegrationConfigRequestDto {
|
||||||
|
@IsEnum(IntegrationType)
|
||||||
|
type: IntegrationType;
|
||||||
|
|
||||||
|
@IsDefined()
|
||||||
|
@ValidateNested()
|
||||||
|
@Type((options) =>
|
||||||
|
options?.object?.type === IntegrationType.DINGTALK
|
||||||
|
? DingTalkThirdConfigDto
|
||||||
|
: WeComThirdConfigDto,
|
||||||
|
)
|
||||||
|
config: DingTalkThirdConfigDto | WeComThirdConfigDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SaveIntegrationConfigDto extends IntegrationConfigRequestDto {}
|
||||||
|
|
||||||
|
export class TestIntegrationConfigDto extends IntegrationConfigRequestDto {}
|
||||||
|
|
||||||
/** 对外返回的配置(脱敏后,不含 appSecret) */
|
/** 对外返回的配置(脱敏后,不含 appSecret) */
|
||||||
export interface ThirdConfigBaseDTO<T = unknown> {
|
export interface ThirdConfigBaseDTO<T = unknown> {
|
||||||
type: string;
|
type: string;
|
||||||
verify?: boolean;
|
verify?: boolean;
|
||||||
config: T;
|
config: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 保存配置的请求体 */
|
|
||||||
export interface SaveConfigRequest {
|
|
||||||
type: 'WECOM' | 'DINGTALK';
|
|
||||||
config: DingTalkThirdConfig | WeComThirdConfig;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common';
|
|||||||
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
|
||||||
import { RequirePermission } from '../../auth/decorators/permission.decorator';
|
import { RequirePermission } from '../../auth/decorators/permission.decorator';
|
||||||
import { IntegrationConfigService } from './integration-config.service';
|
import { IntegrationConfigService } from './integration-config.service';
|
||||||
import type { SaveConfigRequest } from './dto/config.dto';
|
import { SaveIntegrationConfigDto, TestIntegrationConfigDto } from './dto/config.dto';
|
||||||
|
|
||||||
@Controller('integration/config')
|
@Controller('integration/config')
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@@ -31,7 +31,7 @@ export class IntegrationConfigController {
|
|||||||
/** 保存配置 */
|
/** 保存配置 */
|
||||||
@Post()
|
@Post()
|
||||||
@RequirePermission('integration:trigger')
|
@RequirePermission('integration:trigger')
|
||||||
async saveConfig(@Body() body: SaveConfigRequest) {
|
async saveConfig(@Body() body: SaveIntegrationConfigDto) {
|
||||||
await this.service.saveConfig(body);
|
await this.service.saveConfig(body);
|
||||||
return { success: true, message: '配置已保存' };
|
return { success: true, message: '配置已保存' };
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ export class IntegrationConfigController {
|
|||||||
/** 测试连接 */
|
/** 测试连接 */
|
||||||
@Post('test')
|
@Post('test')
|
||||||
@RequirePermission('integration:read')
|
@RequirePermission('integration:read')
|
||||||
async testConnection(@Body() body: SaveConfigRequest) {
|
async testConnection(@Body() body: TestIntegrationConfigDto) {
|
||||||
const success = await this.service.testConnection(body.type, body.config);
|
const success = await this.service.testConnection(body.type, body.config);
|
||||||
return { success, message: success ? '连接成功' : '连接失败,请检查配置信息' };
|
return { success, message: success ? '连接成功' : '连接失败,请检查配置信息' };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import { IntegrationConfigService } from './integration-config.service';
|
||||||
|
|
||||||
|
describe('IntegrationConfigService.testConnection', () => {
|
||||||
|
const originalFetch = global.fetch;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
global.fetch = originalFetch;
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the saved AppSecret when testing an existing configuration with a blank secret', async () => {
|
||||||
|
const configRepo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue({ id: 1, type: 'THIRD' }),
|
||||||
|
};
|
||||||
|
const detailRepo = {
|
||||||
|
findOne: jest.fn().mockResolvedValue({
|
||||||
|
configId: 1,
|
||||||
|
type: 'DINGTALK_SYNC',
|
||||||
|
content: JSON.stringify({
|
||||||
|
config: {
|
||||||
|
corpId: 'ding-corp',
|
||||||
|
agentId: 'saved-key',
|
||||||
|
appSecret: 'saved-secret',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
global.fetch = jest.fn().mockResolvedValue({
|
||||||
|
json: jest.fn().mockResolvedValue({ accessToken: 'token' }),
|
||||||
|
}) as never;
|
||||||
|
|
||||||
|
const service = new IntegrationConfigService(configRepo as never, detailRepo as never);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
service.testConnection('DINGTALK', {
|
||||||
|
corpId: 'ding-corp',
|
||||||
|
agentId: 'saved-key',
|
||||||
|
appSecret: '',
|
||||||
|
}),
|
||||||
|
).resolves.toBe(true);
|
||||||
|
|
||||||
|
expect(global.fetch).toHaveBeenCalledWith(
|
||||||
|
'https://api.dingtalk.com/v1.0/oauth2/accessToken',
|
||||||
|
expect.objectContaining({
|
||||||
|
body: JSON.stringify({ appKey: 'saved-key', appSecret: 'saved-secret' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,15 +1,13 @@
|
|||||||
import { Injectable, Logger, BadRequestException, NotFoundException } from '@nestjs/common';
|
import { Injectable, Logger, BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import {
|
import { IntegrationConfig, IntegrationConfigDetail } from '../entities/integration-config.entity';
|
||||||
IntegrationConfig,
|
|
||||||
IntegrationConfigDetail,
|
|
||||||
} from '../entities/integration-config.entity';
|
|
||||||
import {
|
import {
|
||||||
ThirdConfigBaseDTO,
|
ThirdConfigBaseDTO,
|
||||||
DingTalkThirdConfig,
|
DingTalkThirdConfigDto,
|
||||||
WeComThirdConfig,
|
WeComThirdConfigDto,
|
||||||
SaveConfigRequest,
|
IntegrationType,
|
||||||
|
SaveIntegrationConfigDto,
|
||||||
} from './dto/config.dto';
|
} from './dto/config.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -67,7 +65,7 @@ export class IntegrationConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 保存/更新配置 */
|
/** 保存/更新配置 */
|
||||||
async saveConfig(request: SaveConfigRequest): Promise<void> {
|
async saveConfig(request: SaveIntegrationConfigDto): Promise<void> {
|
||||||
const config = await this.ensureConfig();
|
const config = await this.ensureConfig();
|
||||||
const detailType = this.getDetailType(request.type);
|
const detailType = this.getDetailType(request.type);
|
||||||
|
|
||||||
@@ -121,11 +119,16 @@ export class IntegrationConfigService {
|
|||||||
|
|
||||||
/** 测试连接 */
|
/** 测试连接 */
|
||||||
async testConnection(
|
async testConnection(
|
||||||
type: string,
|
type: IntegrationType,
|
||||||
config: DingTalkThirdConfig | WeComThirdConfig,
|
config: DingTalkThirdConfigDto | WeComThirdConfigDto,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const token = await this.getTokenForTest(type, config as unknown as Record<string, unknown>);
|
const finalConfig = { ...config } as Record<string, unknown>;
|
||||||
|
if (!finalConfig.appSecret) {
|
||||||
|
const savedConfig = await this.getRawConfig(type);
|
||||||
|
if (savedConfig?.appSecret) finalConfig.appSecret = savedConfig.appSecret;
|
||||||
|
}
|
||||||
|
const token = await this.getTokenForTest(type, finalConfig);
|
||||||
return !!token;
|
return !!token;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`连接测试失败: ${(e as Error).message}`);
|
this.logger.error(`连接测试失败: ${(e as Error).message}`);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe('DingTalkService attendance group deletion', () => {
|
|||||||
service = new DingTalkService({} as never, {} as never);
|
service = new DingTalkService({} as never, {} as never);
|
||||||
Object.assign(service, {
|
Object.assign(service, {
|
||||||
accessToken: 'test-token',
|
accessToken: 'test-token',
|
||||||
|
accessTokenCredentialKey: 'test-app-key:test-app-secret',
|
||||||
tokenExpiresAt: Date.now() + 3_600_000,
|
tokenExpiresAt: Date.now() + 3_600_000,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ describe('DingTalkService — queryShifts', () => {
|
|||||||
service = new DingTalkService({} as never, {} as never);
|
service = new DingTalkService({} as never, {} as never);
|
||||||
Object.assign(service, {
|
Object.assign(service, {
|
||||||
accessToken: 'test-token',
|
accessToken: 'test-token',
|
||||||
|
accessTokenCredentialKey: 'test-app-key:test-app-secret',
|
||||||
tokenExpiresAt: Date.now() + 3_600_000,
|
tokenExpiresAt: Date.now() + 3_600_000,
|
||||||
});
|
});
|
||||||
jest.spyOn(service, 'rateLimit').mockResolvedValue(undefined);
|
jest.spyOn(service, 'rateLimit').mockResolvedValue(undefined);
|
||||||
@@ -105,6 +106,7 @@ describe('DingTalkService — attendance machine only group', () => {
|
|||||||
service = new DingTalkService({} as never, {} as never);
|
service = new DingTalkService({} as never, {} as never);
|
||||||
Object.assign(service, {
|
Object.assign(service, {
|
||||||
accessToken: 'test-token',
|
accessToken: 'test-token',
|
||||||
|
accessTokenCredentialKey: 'test-app-key:test-app-secret',
|
||||||
tokenExpiresAt: Date.now() + 3_600_000,
|
tokenExpiresAt: Date.now() + 3_600_000,
|
||||||
});
|
});
|
||||||
jest.spyOn(service, 'rateLimit').mockResolvedValue(undefined);
|
jest.spyOn(service, 'rateLimit').mockResolvedValue(undefined);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { Student } from '../entities/student.entity';
|
import { Student } from '../entities/student.entity';
|
||||||
import { StudentDingMapping } from '../entities/student-ding-mapping.entity';
|
import { StudentDingMapping } from '../entities/student-ding-mapping.entity';
|
||||||
|
import { IntegrationConfigService } from './config/integration-config.service';
|
||||||
|
|
||||||
// ── Types ──
|
// ── Types ──
|
||||||
|
|
||||||
@@ -18,6 +19,11 @@ interface DingTalkTokenResponse {
|
|||||||
expireIn: number;
|
expireIn: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DingTalkCredentials {
|
||||||
|
appKey: string;
|
||||||
|
appSecret: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface DingTalkUserListResponse {
|
interface DingTalkUserListResponse {
|
||||||
errcode: number;
|
errcode: number;
|
||||||
errmsg: string;
|
errmsg: string;
|
||||||
@@ -168,6 +174,7 @@ export interface DingTalkScheduleResult {
|
|||||||
export class DingTalkService {
|
export class DingTalkService {
|
||||||
private readonly logger = new Logger(DingTalkService.name);
|
private readonly logger = new Logger(DingTalkService.name);
|
||||||
private accessToken: string | null = null;
|
private accessToken: string | null = null;
|
||||||
|
private accessTokenCredentialKey: string | null = null;
|
||||||
private tokenExpiresAt = 0;
|
private tokenExpiresAt = 0;
|
||||||
private apiRequestCount = 0;
|
private apiRequestCount = 0;
|
||||||
|
|
||||||
@@ -180,10 +187,28 @@ export class DingTalkService {
|
|||||||
private readonly studentRepo: Repository<Student>,
|
private readonly studentRepo: Repository<Student>,
|
||||||
@InjectRepository(StudentDingMapping)
|
@InjectRepository(StudentDingMapping)
|
||||||
private readonly studentDingMappingRepo: Repository<StudentDingMapping>,
|
private readonly studentDingMappingRepo: Repository<StudentDingMapping>,
|
||||||
|
private readonly integrationConfigService?: IntegrationConfigService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
private get configured(): boolean {
|
private async getCredentials(): Promise<DingTalkCredentials | null> {
|
||||||
return !!(process.env.DINGTALK_APP_KEY && process.env.DINGTALK_APP_SECRET);
|
const rawConfig = await this.integrationConfigService?.getRawConfig('DINGTALK');
|
||||||
|
const dbAppKey = typeof rawConfig?.agentId === 'string' ? rawConfig.agentId.trim() : '';
|
||||||
|
const dbAppSecret = typeof rawConfig?.appSecret === 'string' ? rawConfig.appSecret.trim() : '';
|
||||||
|
if (dbAppKey && dbAppSecret) {
|
||||||
|
return { appKey: dbAppKey, appSecret: dbAppSecret };
|
||||||
|
}
|
||||||
|
|
||||||
|
const envAppKey = process.env.DINGTALK_APP_KEY?.trim();
|
||||||
|
const envAppSecret = process.env.DINGTALK_APP_SECRET?.trim();
|
||||||
|
if (envAppKey && envAppSecret) {
|
||||||
|
return { appKey: envAppKey, appSecret: envAppSecret };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async isConfigured(): Promise<boolean> {
|
||||||
|
return !!(await this.getCredentials());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ═══════════════════════════════════════════
|
// ═══════════════════════════════════════════
|
||||||
@@ -191,16 +216,24 @@ export class DingTalkService {
|
|||||||
// ═══════════════════════════════════════════
|
// ═══════════════════════════════════════════
|
||||||
|
|
||||||
private async getAccessToken(): Promise<string> {
|
private async getAccessToken(): Promise<string> {
|
||||||
if (this.accessToken && Date.now() < this.tokenExpiresAt - 60_000) {
|
const credentials = await this.getCredentials();
|
||||||
|
if (!credentials) {
|
||||||
|
throw new Error('DingTalk not configured');
|
||||||
|
}
|
||||||
|
|
||||||
|
const credentialKey = `${credentials.appKey}:${credentials.appSecret}`;
|
||||||
|
if (
|
||||||
|
this.accessToken &&
|
||||||
|
this.accessTokenCredentialKey === credentialKey &&
|
||||||
|
Date.now() < this.tokenExpiresAt - 60_000
|
||||||
|
) {
|
||||||
return this.accessToken;
|
return this.accessToken;
|
||||||
}
|
}
|
||||||
const appKey = process.env.DINGTALK_APP_KEY!;
|
|
||||||
const appSecret = process.env.DINGTALK_APP_SECRET!;
|
|
||||||
|
|
||||||
const res = await fetch('https://api.dingtalk.com/v1.0/oauth2/accessToken', {
|
const res = await fetch('https://api.dingtalk.com/v1.0/oauth2/accessToken', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ appKey, appSecret }),
|
body: JSON.stringify(credentials),
|
||||||
});
|
});
|
||||||
const body: DingTalkTokenResponse = await res.json();
|
const body: DingTalkTokenResponse = await res.json();
|
||||||
|
|
||||||
@@ -209,6 +242,7 @@ export class DingTalkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.accessToken = body.accessToken;
|
this.accessToken = body.accessToken;
|
||||||
|
this.accessTokenCredentialKey = credentialKey;
|
||||||
this.tokenExpiresAt = Date.now() + (body.expireIn || 7200) * 1000;
|
this.tokenExpiresAt = Date.now() + (body.expireIn || 7200) * 1000;
|
||||||
this.logger.log('钉钉 access_token 获取成功');
|
this.logger.log('钉钉 access_token 获取成功');
|
||||||
return this.accessToken;
|
return this.accessToken;
|
||||||
@@ -259,7 +293,7 @@ export class DingTalkService {
|
|||||||
// ═══════════════════════════════════════════
|
// ═══════════════════════════════════════════
|
||||||
|
|
||||||
async syncAll(rootDeptId = 1): Promise<{ deptCount: number; userCount: number }> {
|
async syncAll(rootDeptId = 1): Promise<{ deptCount: number; userCount: number }> {
|
||||||
if (!this.configured) {
|
if (!(await this.isConfigured())) {
|
||||||
this.logger.warn('钉钉未配置 (DINGTALK_APP_KEY / DINGTALK_APP_SECRET),跳过同步');
|
this.logger.warn('钉钉未配置 (DINGTALK_APP_KEY / DINGTALK_APP_SECRET),跳过同步');
|
||||||
return { deptCount: 0, userCount: 0 };
|
return { deptCount: 0, userCount: 0 };
|
||||||
}
|
}
|
||||||
@@ -333,7 +367,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 获取钉钉组织部门树(只含部门) */
|
/** 获取钉钉组织部门树(只含部门) */
|
||||||
async fetchOrgTree(rootDeptId = 1): Promise<OrgDeptNode[]> {
|
async fetchOrgTree(rootDeptId = 1): Promise<OrgDeptNode[]> {
|
||||||
if (!this.configured) return [];
|
if (!(await this.isConfigured())) return [];
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
const rootInfo = await this.getDeptInfo(token, rootDeptId);
|
const rootInfo = await this.getDeptInfo(token, rootDeptId);
|
||||||
if (!rootInfo) return [];
|
if (!rootInfo) return [];
|
||||||
@@ -343,7 +377,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 获取钉钉组织部门树(含用户) */
|
/** 获取钉钉组织部门树(含用户) */
|
||||||
async fetchOrgTreeWithUsers(rootDeptId = 1): Promise<OrgDeptNodeWithUsers[]> {
|
async fetchOrgTreeWithUsers(rootDeptId = 1): Promise<OrgDeptNodeWithUsers[]> {
|
||||||
if (!this.configured) return [];
|
if (!(await this.isConfigured())) return [];
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
const rootInfo = await this.getDeptInfo(token, rootDeptId);
|
const rootInfo = await this.getDeptInfo(token, rootDeptId);
|
||||||
if (!rootInfo) return [];
|
if (!rootInfo) return [];
|
||||||
@@ -441,7 +475,7 @@ export class DingTalkService {
|
|||||||
endDate: string;
|
endDate: string;
|
||||||
userIds?: string[];
|
userIds?: string[];
|
||||||
}): Promise<DingTalkAttendanceResult[]> {
|
}): Promise<DingTalkAttendanceResult[]> {
|
||||||
if (!this.configured) throw new Error('DingTalk not configured');
|
if (!(await this.isConfigured())) throw new Error('DingTalk not configured');
|
||||||
if (!params.userIds?.length) throw new Error('钉钉考勤 userIds 不能为空');
|
if (!params.userIds?.length) throw new Error('钉钉考勤 userIds 不能为空');
|
||||||
if (params.userIds.length > 50) throw new Error('钉钉考勤单次最多查询50人');
|
if (params.userIds.length > 50) throw new Error('钉钉考勤单次最多查询50人');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
@@ -496,7 +530,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 创建或修改班次。id 不传=创建,传了=修改 */
|
/** 创建或修改班次。id 不传=创建,传了=修改 */
|
||||||
async upsertShift(params: DingTalkShiftParams): Promise<number> {
|
async upsertShift(params: DingTalkShiftParams): Promise<number> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
const body: Record<string, unknown> = {
|
const body: Record<string, unknown> = {
|
||||||
@@ -547,7 +581,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 查询所有班次摘要(每页最多200条) */
|
/** 查询所有班次摘要(每页最多200条) */
|
||||||
async queryShifts(opUserId = 'manager'): Promise<DingTalkShiftSummary[]> {
|
async queryShifts(opUserId = 'manager'): Promise<DingTalkShiftSummary[]> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
const all: DingTalkShiftSummary[] = [];
|
const all: DingTalkShiftSummary[] = [];
|
||||||
@@ -598,7 +632,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 创建排班制考勤组 */
|
/** 创建排班制考勤组 */
|
||||||
async createAttendanceGroup(params: DingTalkGroupParams): Promise<number> {
|
async createAttendanceGroup(params: DingTalkGroupParams): Promise<number> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
const topGroup = this.buildAttendanceGroupBody(params);
|
const topGroup = this.buildAttendanceGroupBody(params);
|
||||||
@@ -627,7 +661,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 更新排班制考勤组,确保复用考勤组时同步最新打卡限制 */
|
/** 更新排班制考勤组,确保复用考勤组时同步最新打卡限制 */
|
||||||
async updateAttendanceGroup(params: DingTalkGroupUpdateParams): Promise<void> {
|
async updateAttendanceGroup(params: DingTalkGroupUpdateParams): Promise<void> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
const topGroup = { ...this.buildAttendanceGroupBody(params), id: params.id };
|
const topGroup = { ...this.buildAttendanceGroupBody(params), id: params.id };
|
||||||
|
|
||||||
@@ -687,7 +721,7 @@ export class DingTalkService {
|
|||||||
|
|
||||||
/** 查询所有考勤组摘要(分页,每页10条) */
|
/** 查询所有考勤组摘要(分页,每页10条) */
|
||||||
async queryAttendanceGroups(opUserId = 'manager'): Promise<DingTalkGroupSummary[]> {
|
async queryAttendanceGroups(opUserId = 'manager'): Promise<DingTalkGroupSummary[]> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
const all: DingTalkGroupSummary[] = [];
|
const all: DingTalkGroupSummary[] = [];
|
||||||
@@ -729,7 +763,7 @@ export class DingTalkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async deleteAttendanceGroup(groupId: number, opUserId = 'manager'): Promise<void> {
|
async deleteAttendanceGroup(groupId: number, opUserId = 'manager'): Promise<void> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
await this.rateLimit();
|
await this.rateLimit();
|
||||||
@@ -778,7 +812,7 @@ export class DingTalkService {
|
|||||||
async scheduleUsers(
|
async scheduleUsers(
|
||||||
groupId: number, schedules: DingTalkScheduleItem[], opUserId = 'manager',
|
groupId: number, schedules: DingTalkScheduleItem[], opUserId = 'manager',
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
if (schedules.length === 0) return;
|
if (schedules.length === 0) return;
|
||||||
if (schedules.length > 200) {
|
if (schedules.length > 200) {
|
||||||
throw new Error(`排班单次最多200条,当前 ${schedules.length} 条`);
|
throw new Error(`排班单次最多200条,当前 ${schedules.length} 条`);
|
||||||
@@ -818,7 +852,7 @@ export class DingTalkService {
|
|||||||
async queryScheduleByUsers(
|
async queryScheduleByUsers(
|
||||||
userIds: string[], fromDate: number, toDate: number, opUserId = 'manager',
|
userIds: string[], fromDate: number, toDate: number, opUserId = 'manager',
|
||||||
): Promise<DingTalkScheduleResult[]> {
|
): Promise<DingTalkScheduleResult[]> {
|
||||||
if (!this.configured) throw new ServiceUnavailableException('钉钉未配置');
|
if (!(await this.isConfigured())) throw new ServiceUnavailableException('钉钉未配置');
|
||||||
const token = await this.getAccessToken();
|
const token = await this.getAccessToken();
|
||||||
|
|
||||||
await this.rateLimit();
|
await this.rateLimit();
|
||||||
|
|||||||
@@ -3,9 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
|||||||
import { User, Student, StudentDingMapping, Class } from '../entities';
|
import { User, Student, StudentDingMapping, Class } from '../entities';
|
||||||
import { DingTalkService } from './dingtalk.service';
|
import { DingTalkService } from './dingtalk.service';
|
||||||
import { WeComService } from './wecom.service';
|
import { WeComService } from './wecom.service';
|
||||||
|
import { IntegrationConfigModule } from './config/config.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TypeOrmModule.forFeature([User, Student, StudentDingMapping, Class])],
|
imports: [
|
||||||
|
TypeOrmModule.forFeature([User, Student, StudentDingMapping, Class]),
|
||||||
|
IntegrationConfigModule,
|
||||||
|
],
|
||||||
providers: [DingTalkService, WeComService],
|
providers: [DingTalkService, WeComService],
|
||||||
exports: [DingTalkService, WeComService],
|
exports: [DingTalkService, WeComService],
|
||||||
})
|
})
|
||||||
|
|||||||
28
apps/server/src/occupancies/dto/occupancy.dto.spec.ts
Normal file
28
apps/server/src/occupancies/dto/occupancy.dto.spec.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { validate } from 'class-validator';
|
||||||
|
import { CheckInDto, TransferRoomDto } from './occupancy.dto';
|
||||||
|
|
||||||
|
describe('manual occupancy DTO bed requirements', () => {
|
||||||
|
it('requires a bed for manual check-in', async () => {
|
||||||
|
const dto = Object.assign(new CheckInDto(), {
|
||||||
|
studentId: 1,
|
||||||
|
roomId: 2,
|
||||||
|
checkInDate: '2026-07-13',
|
||||||
|
});
|
||||||
|
|
||||||
|
const errors = await validate(dto);
|
||||||
|
|
||||||
|
expect(errors.some((error) => error.property === 'bedId')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('requires a new bed for a room transfer while keeping the locker optional', async () => {
|
||||||
|
const dto = Object.assign(new TransferRoomDto(), {
|
||||||
|
newRoomId: 3,
|
||||||
|
transferDate: '2026-07-13',
|
||||||
|
});
|
||||||
|
|
||||||
|
const errors = await validate(dto);
|
||||||
|
|
||||||
|
expect(errors.some((error) => error.property === 'newBedId')).toBe(true);
|
||||||
|
expect(errors.some((error) => error.property === 'newLockerId')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -25,9 +25,8 @@ export class CheckInDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
responsibleOrganizationId?: number;
|
responsibleOrganizationId?: number;
|
||||||
@IsOptional()
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
bedId?: number; // 后续改 required
|
bedId: number;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@@ -58,9 +57,8 @@ export class TransferRoomDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
oldBillingEndDate?: string; // 旧房计费截止日,默认=transferDate
|
oldBillingEndDate?: string; // 旧房计费截止日,默认=transferDate
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
newBedId?: number;
|
newBedId: number;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
|
|||||||
@@ -300,8 +300,7 @@ export class OccupanciesController {
|
|||||||
helpWs.addRow(['4. 已存在的学生(按姓名匹配)会自动补充缺失信息(性别、民族等)']);
|
helpWs.addRow(['4. 已存在的学生(按姓名匹配)会自动补充缺失信息(性别、民族等)']);
|
||||||
helpWs.addRow(['5. 已有在住记录的学生会自动跳过,不会重复入住']);
|
helpWs.addRow(['5. 已有在住记录的学生会自动跳过,不会重复入住']);
|
||||||
helpWs.addRow(['6. 填了离宿时间的记录会直接标记为已退宿(用于导入历史数据)']);
|
helpWs.addRow(['6. 填了离宿时间的记录会直接标记为已退宿(用于导入历史数据)']);
|
||||||
helpWs.addRow(['7. 性别约束:同一宿舍只能住同性别学生,首位入住者确定宿舍性别']);
|
helpWs.addRow(['7. 床位号仅做标识参考,不影响入住逻辑']);
|
||||||
helpWs.addRow(['8. 床位号仅做标识参考,不影响入住逻辑']);
|
|
||||||
helpWs.getRow(1).font = { bold: true, size: 14 };
|
helpWs.getRow(1).font = { bold: true, size: 14 };
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
'Content-Type',
|
'Content-Type',
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ describe('OccupanciesService — responsible organization', () => {
|
|||||||
save: jest.fn(async (value) => ({ ...value, id: 10 })),
|
save: jest.fn(async (value) => ({ ...value, id: 10 })),
|
||||||
} as any as Repository<Occupancy>;
|
} as any as Repository<Occupancy>;
|
||||||
const roomRepo = {
|
const roomRepo = {
|
||||||
findOne: jest.fn().mockResolvedValue({ id: 2, capacity: 4, gender: null }),
|
findOne: jest.fn().mockResolvedValue({ id: 2, capacity: 4 }),
|
||||||
update: jest.fn(),
|
update: jest.fn(),
|
||||||
} as any as Repository<Room>;
|
} as any as Repository<Room>;
|
||||||
const studentRepo = {
|
const studentRepo = {
|
||||||
@@ -28,7 +28,10 @@ describe('OccupanciesService — responsible organization', () => {
|
|||||||
roomRepo,
|
roomRepo,
|
||||||
studentRepo,
|
studentRepo,
|
||||||
{} as Repository<Deposit>,
|
{} as Repository<Deposit>,
|
||||||
{} as Repository<Bed>,
|
{
|
||||||
|
findOne: jest.fn().mockResolvedValue({ id: 4, roomId: 2, status: 'available' }),
|
||||||
|
update: jest.fn(),
|
||||||
|
} as any as Repository<Bed>,
|
||||||
{} as Repository<Locker>,
|
{} as Repository<Locker>,
|
||||||
{} as Repository<any>,
|
{} as Repository<any>,
|
||||||
{} as DataSource,
|
{} as DataSource,
|
||||||
@@ -38,6 +41,7 @@ describe('OccupanciesService — responsible organization', () => {
|
|||||||
studentId: 3,
|
studentId: 3,
|
||||||
roomId: 2,
|
roomId: 2,
|
||||||
checkInDate: '2026-07-10',
|
checkInDate: '2026-07-10',
|
||||||
|
bedId: 4,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(occupancyRepo.create).toHaveBeenCalledWith(
|
expect(occupancyRepo.create).toHaveBeenCalledWith(
|
||||||
|
|||||||
@@ -61,14 +61,8 @@ export class OccupanciesService {
|
|||||||
const count = await this.repo.count({ where: { roomId: dto.roomId, checkOutDate: IsNull() } });
|
const count = await this.repo.count({ where: { roomId: dto.roomId, checkOutDate: IsNull() } });
|
||||||
if (count >= room.capacity) throw new BadRequestException('宿舍已满');
|
if (count >= room.capacity) throw new BadRequestException('宿舍已满');
|
||||||
|
|
||||||
// 房间级别性别约束
|
|
||||||
const student = await this.studentRepo.findOne({ where: { id: dto.studentId } });
|
const student = await this.studentRepo.findOne({ where: { id: dto.studentId } });
|
||||||
if (!student) throw new NotFoundException('学生不存在');
|
if (!student) throw new NotFoundException('学生不存在');
|
||||||
if (student.gender && room.gender && student.gender !== room.gender) {
|
|
||||||
throw new BadRequestException(
|
|
||||||
`该宿舍当前为${room.gender}生寝室,${student.gender}生无法入住`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 床位校验
|
// 床位校验
|
||||||
if (dto.bedId) {
|
if (dto.bedId) {
|
||||||
@@ -107,11 +101,6 @@ export class OccupanciesService {
|
|||||||
await this.lockerRepo.update(dto.lockerId, { status: 'occupied' });
|
await this.lockerRepo.update(dto.lockerId, { status: 'occupied' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首位入住者确定房间性别
|
|
||||||
if (student.gender && !room.gender) {
|
|
||||||
await this.roomRepo.update(room.id, { gender: student.gender });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新宿舍状态
|
// 更新宿舍状态
|
||||||
if (count + 1 >= room.capacity) {
|
if (count + 1 >= room.capacity) {
|
||||||
await this.roomRepo.update(room.id, { status: 'full' });
|
await this.roomRepo.update(room.id, { status: 'full' });
|
||||||
@@ -140,14 +129,6 @@ export class OccupanciesService {
|
|||||||
// 更新宿舍状态
|
// 更新宿舍状态
|
||||||
await this.roomRepo.update(occ.roomId, { status: 'available' });
|
await this.roomRepo.update(occ.roomId, { status: 'available' });
|
||||||
|
|
||||||
// 如果房间已无在住人员,重置房间性别
|
|
||||||
const remaining = await this.repo.count({
|
|
||||||
where: { roomId: occ.roomId, checkOutDate: IsNull() },
|
|
||||||
});
|
|
||||||
if (remaining === 0) {
|
|
||||||
await this.roomRepo.update(occ.roomId, { gender: null as any });
|
|
||||||
}
|
|
||||||
|
|
||||||
return occ;
|
return occ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,14 +154,6 @@ export class OccupanciesService {
|
|||||||
await runner.manager.update(Locker, oldOcc.lockerId, { status: 'available' });
|
await runner.manager.update(Locker, oldOcc.lockerId, { status: 'available' });
|
||||||
}
|
}
|
||||||
await runner.manager.update(Room, oldOcc.roomId, { status: 'available' });
|
await runner.manager.update(Room, oldOcc.roomId, { status: 'available' });
|
||||||
// 旧房如果已无在住人员,重置性别
|
|
||||||
const oldRemaining = await runner.manager.count(Occupancy, {
|
|
||||||
where: { roomId: oldOcc.roomId, checkOutDate: IsNull() },
|
|
||||||
});
|
|
||||||
if (oldRemaining === 0) {
|
|
||||||
await runner.manager.update(Room, oldOcc.roomId, { gender: null as any });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查新房容量
|
// 检查新房容量
|
||||||
const newRoom = await runner.manager.findOne(Room, { where: { id: dto.newRoomId } });
|
const newRoom = await runner.manager.findOne(Room, { where: { id: dto.newRoomId } });
|
||||||
if (!newRoom) throw new NotFoundException('目标宿舍不存在');
|
if (!newRoom) throw new NotFoundException('目标宿舍不存在');
|
||||||
@@ -189,12 +162,6 @@ export class OccupanciesService {
|
|||||||
});
|
});
|
||||||
if (count >= newRoom.capacity) throw new BadRequestException('目标宿舍已满');
|
if (count >= newRoom.capacity) throw new BadRequestException('目标宿舍已满');
|
||||||
|
|
||||||
// 换房性别约束检查
|
|
||||||
const student = await runner.manager.findOne(Student, { where: { id: oldOcc.studentId } });
|
|
||||||
if (student?.gender && newRoom.gender && student.gender !== newRoom.gender) {
|
|
||||||
throw new BadRequestException(`目标宿舍为${newRoom.gender}生寝室,无法换入`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新床位校验
|
// 新床位校验
|
||||||
if (dto.newBedId) {
|
if (dto.newBedId) {
|
||||||
const newBed = await runner.manager.findOne(Bed, {
|
const newBed = await runner.manager.findOne(Bed, {
|
||||||
@@ -239,11 +206,6 @@ export class OccupanciesService {
|
|||||||
await runner.manager.update(Locker, dto.newLockerId, { status: 'occupied' });
|
await runner.manager.update(Locker, dto.newLockerId, { status: 'occupied' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首位入住者确定新房性别
|
|
||||||
if (student?.gender && !newRoom.gender) {
|
|
||||||
await runner.manager.update(Room, newRoom.id, { gender: student.gender });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count + 1 >= newRoom.capacity) {
|
if (count + 1 >= newRoom.capacity) {
|
||||||
await runner.manager.update(Room, newRoom.id, { status: 'full' });
|
await runner.manager.update(Room, newRoom.id, { status: 'full' });
|
||||||
}
|
}
|
||||||
@@ -339,13 +301,6 @@ export class OccupanciesService {
|
|||||||
await runner.manager.save(occ);
|
await runner.manager.save(occ);
|
||||||
// 更新房间状态
|
// 更新房间状态
|
||||||
await runner.manager.update(Room, occ.roomId, { status: 'available' });
|
await runner.manager.update(Room, occ.roomId, { status: 'available' });
|
||||||
// 如果房间已无在住人员,重置性别
|
|
||||||
const remaining = await runner.manager.count(Occupancy, {
|
|
||||||
where: { roomId: occ.roomId, checkOutDate: IsNull() },
|
|
||||||
});
|
|
||||||
if (remaining === 0) {
|
|
||||||
await runner.manager.update(Room, occ.roomId, { gender: null as any });
|
|
||||||
}
|
|
||||||
// 释放床位/柜子
|
// 释放床位/柜子
|
||||||
if (occ.bedId) await runner.manager.update(Bed, occ.bedId, { status: 'available' });
|
if (occ.bedId) await runner.manager.update(Bed, occ.bedId, { status: 'available' });
|
||||||
if (occ.lockerId)
|
if (occ.lockerId)
|
||||||
@@ -495,15 +450,6 @@ export class OccupanciesService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 房间级别性别约束
|
|
||||||
if (student.gender && room.gender && student.gender !== room.gender) {
|
|
||||||
errors.push(
|
|
||||||
`第${rowNum}行: 宿舍 ${row.roomNumber} 为${room.gender}生寝室,${row.name}(${student.gender})无法入住,跳过`,
|
|
||||||
);
|
|
||||||
skipped++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. 创建入住记录
|
// 6. 创建入住记录
|
||||||
const checkInDate = row.checkInDate?.trim() || new Date().toISOString().split('T')[0];
|
const checkInDate = row.checkInDate?.trim() || new Date().toISOString().split('T')[0];
|
||||||
const occData: any = {
|
const occData: any = {
|
||||||
@@ -520,12 +466,6 @@ export class OccupanciesService {
|
|||||||
}
|
}
|
||||||
await this.repo.save(this.repo.create(occData));
|
await this.repo.save(this.repo.create(occData));
|
||||||
|
|
||||||
// 7. 首位入住者确定房间性别
|
|
||||||
if (student.gender && !room.gender) {
|
|
||||||
await this.roomRepo.update(room.id, { gender: student.gender });
|
|
||||||
room.gender = student.gender;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 8. 更新宿舍状态
|
// 8. 更新宿舍状态
|
||||||
if (!row.checkOutDate?.trim() && count + 1 >= room.capacity) {
|
if (!row.checkOutDate?.trim() && count + 1 >= room.capacity) {
|
||||||
await this.roomRepo.update(room.id, { status: 'full' });
|
await this.roomRepo.update(room.id, { status: 'full' });
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ export class CreateRoomDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
monthlyRate?: number;
|
monthlyRate?: number;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateRoomDto {
|
export class UpdateRoomDto {
|
||||||
@@ -52,10 +51,6 @@ export class UpdateRoomDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
roomType?: string;
|
roomType?: string;
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
gender?: string;
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsEnum(['available', 'full', 'maintenance'])
|
@IsEnum(['available', 'full', 'maintenance'])
|
||||||
status?: string;
|
status?: string;
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ export class RoomsController {
|
|||||||
{ header: '宿舍类型', key: 'roomType', width: 12 },
|
{ header: '宿舍类型', key: 'roomType', width: 12 },
|
||||||
{ header: '额定人数', key: 'capacity', width: 10 },
|
{ header: '额定人数', key: 'capacity', width: 10 },
|
||||||
{ header: '当前入住', key: 'currentCount', width: 10 },
|
{ header: '当前入住', key: 'currentCount', width: 10 },
|
||||||
{ header: '性别', key: 'gender', width: 8 },
|
|
||||||
{ header: '状态', key: 'status', width: 10 },
|
{ header: '状态', key: 'status', width: 10 },
|
||||||
{ header: '租赁类型', key: 'rentalCategory', width: 12 },
|
{ header: '租赁类型', key: 'rentalCategory', width: 12 },
|
||||||
{ header: '月租金', key: 'monthlyRate', width: 10 },
|
{ header: '月租金', key: 'monthlyRate', width: 10 },
|
||||||
@@ -131,7 +130,6 @@ export class RoomsController {
|
|||||||
roomType: r.roomType || '',
|
roomType: r.roomType || '',
|
||||||
capacity: r.capacity,
|
capacity: r.capacity,
|
||||||
currentCount: r.currentCount,
|
currentCount: r.currentCount,
|
||||||
gender: r.gender || '',
|
|
||||||
status: statusMap[r.status] || r.status,
|
status: statusMap[r.status] || r.status,
|
||||||
rentalCategory: r.rentalCategory === 'long' ? '长租' : '短租',
|
rentalCategory: r.rentalCategory === 'long' ? '长租' : '短租',
|
||||||
monthlyRate: r.monthlyRate ?? '',
|
monthlyRate: r.monthlyRate ?? '',
|
||||||
@@ -168,11 +166,7 @@ export class RoomsController {
|
|||||||
|
|
||||||
@Put(':roomId/beds/:id')
|
@Put(':roomId/beds/:id')
|
||||||
@RequirePermission('room:edit')
|
@RequirePermission('room:edit')
|
||||||
updateBed(
|
updateBed(@Param('roomId') roomId: string, @Param('id') id: string, @Body() dto: UpdateBedDto) {
|
||||||
@Param('roomId') roomId: string,
|
|
||||||
@Param('id') id: string,
|
|
||||||
@Body() dto: UpdateBedDto,
|
|
||||||
) {
|
|
||||||
return this.service.updateBed(+roomId, +id, dto);
|
return this.service.updateBed(+roomId, +id, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,7 +337,9 @@ export class RoomsController {
|
|||||||
}[] = [];
|
}[] = [];
|
||||||
ws.eachRow((row, idx) => {
|
ws.eachRow((row, idx) => {
|
||||||
if (idx === 1) return;
|
if (idx === 1) return;
|
||||||
const rentalCategoryRaw = String(row.getCell(6).value || '').trim().toLowerCase();
|
const rentalCategoryRaw = String(row.getCell(6).value || '')
|
||||||
|
.trim()
|
||||||
|
.toLowerCase();
|
||||||
const rentalCategory =
|
const rentalCategory =
|
||||||
rentalCategoryRaw === 'long' || rentalCategoryRaw === 'short'
|
rentalCategoryRaw === 'long' || rentalCategoryRaw === 'short'
|
||||||
? rentalCategoryRaw
|
? rentalCategoryRaw
|
||||||
|
|||||||
@@ -110,7 +110,9 @@ export class RoomsService {
|
|||||||
roomType: dto.roomType ?? parsed.roomType,
|
roomType: dto.roomType ?? parsed.roomType,
|
||||||
capacity: dto.capacity ?? parsed.capacity,
|
capacity: dto.capacity ?? parsed.capacity,
|
||||||
});
|
});
|
||||||
return this.repo.save(entity);
|
const room = await this.repo.save(entity);
|
||||||
|
await this.createDefaultBeds(room.id, room.capacity);
|
||||||
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: number, dto: UpdateRoomDto) {
|
async update(id: number, dto: UpdateRoomDto) {
|
||||||
@@ -202,10 +204,7 @@ export class RoomsService {
|
|||||||
for (const occ of occupancies) {
|
for (const occ of occupancies) {
|
||||||
if (!occMap.has(occ.roomId)) occMap.set(occ.roomId, []);
|
if (!occMap.has(occ.roomId)) occMap.set(occ.roomId, []);
|
||||||
const checkIn = new Date(occ.checkInDate);
|
const checkIn = new Date(occ.checkInDate);
|
||||||
const days = Math.max(
|
const days = Math.max(1, Math.ceil((refTime - checkIn.getTime()) / (1000 * 60 * 60 * 24)));
|
||||||
1,
|
|
||||||
Math.ceil((refTime - checkIn.getTime()) / (1000 * 60 * 60 * 24)),
|
|
||||||
);
|
|
||||||
occMap.get(occ.roomId)!.push({
|
occMap.get(occ.roomId)!.push({
|
||||||
studentId: occ.studentId,
|
studentId: occ.studentId,
|
||||||
studentName: occ.student?.name || '未知',
|
studentName: occ.student?.name || '未知',
|
||||||
@@ -250,8 +249,11 @@ export class RoomsService {
|
|||||||
const allSameOrg = occ.every((o: any) => o.organization && o.organization === orgs[0]);
|
const allSameOrg = occ.every((o: any) => o.organization && o.organization === orgs[0]);
|
||||||
orgLabel = allSameOrg ? `均为${orgs[0]}人员` : `存在${orgs.join('、')}人员`;
|
orgLabel = allSameOrg ? `均为${orgs[0]}人员` : `存在${orgs.join('、')}人员`;
|
||||||
}
|
}
|
||||||
const organizationColors = [...new Set(occ.map((o: any) => o.organizationColor).filter(Boolean))];
|
const organizationColors = [
|
||||||
const organizationColor: string | null = organizationColors.length === 1 ? organizationColors[0] : null;
|
...new Set(occ.map((o: any) => o.organizationColor).filter(Boolean)),
|
||||||
|
];
|
||||||
|
const organizationColor: string | null =
|
||||||
|
organizationColors.length === 1 ? organizationColors[0] : null;
|
||||||
const organizationIds = [...new Set(occ.map((o: any) => o.organizationId).filter(Boolean))];
|
const organizationIds = [...new Set(occ.map((o: any) => o.organizationId).filter(Boolean))];
|
||||||
return {
|
return {
|
||||||
id: room.id,
|
id: room.id,
|
||||||
@@ -274,7 +276,14 @@ export class RoomsService {
|
|||||||
...new Map(
|
...new Map(
|
||||||
occupancies
|
occupancies
|
||||||
.filter((o) => o.responsibleOrganizationId && o.responsibleOrganization)
|
.filter((o) => o.responsibleOrganizationId && o.responsibleOrganization)
|
||||||
.map((o) => [o.responsibleOrganizationId, { id: o.responsibleOrganizationId, name: o.responsibleOrganization.name, color: o.responsibleOrganization.color || null }]),
|
.map((o) => [
|
||||||
|
o.responsibleOrganizationId,
|
||||||
|
{
|
||||||
|
id: o.responsibleOrganizationId,
|
||||||
|
name: o.responsibleOrganization.name,
|
||||||
|
color: o.responsibleOrganization.color || null,
|
||||||
|
},
|
||||||
|
]),
|
||||||
).values(),
|
).values(),
|
||||||
].sort((a, b) => a.name.localeCompare(b.name)),
|
].sort((a, b) => a.name.localeCompare(b.name)),
|
||||||
};
|
};
|
||||||
@@ -305,7 +314,7 @@ export class RoomsService {
|
|||||||
}
|
}
|
||||||
// 智能解析房间号
|
// 智能解析房间号
|
||||||
const parsed = RoomsService.parseRoomNumber(row.roomNumber.trim());
|
const parsed = RoomsService.parseRoomNumber(row.roomNumber.trim());
|
||||||
await this.repo.save(
|
const room = await this.repo.save(
|
||||||
this.repo.create({
|
this.repo.create({
|
||||||
roomNumber: row.roomNumber.trim(),
|
roomNumber: row.roomNumber.trim(),
|
||||||
building: row.building?.trim() || parsed.building || undefined,
|
building: row.building?.trim() || parsed.building || undefined,
|
||||||
@@ -316,6 +325,7 @@ export class RoomsService {
|
|||||||
monthlyRate: row.monthlyRate ?? undefined,
|
monthlyRate: row.monthlyRate ?? undefined,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
await this.createDefaultBeds(room.id, room.capacity);
|
||||||
imported++;
|
imported++;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -336,13 +346,17 @@ export class RoomsService {
|
|||||||
async getRoomAvailableBeds(roomId: number): Promise<Bed[]> {
|
async getRoomAvailableBeds(roomId: number): Promise<Bed[]> {
|
||||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
return this.bedRepo.find({ where: { roomId, status: 'available' }, order: { bedNumber: 'ASC' } });
|
return this.bedRepo.find({
|
||||||
|
where: { roomId, status: 'available' },
|
||||||
|
order: { bedNumber: 'ASC' },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createBed(roomId: number, dto: CreateBedDto): Promise<Bed> {
|
async createBed(roomId: number, dto: CreateBedDto): Promise<Bed> {
|
||||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
||||||
|
await this.assertCanAddBeds(room, 1);
|
||||||
const existing = await this.bedRepo.findOne({ where: { roomId, bedNumber: dto.bedNumber } });
|
const existing = await this.bedRepo.findOne({ where: { roomId, bedNumber: dto.bedNumber } });
|
||||||
if (existing) throw new BadRequestException('该床位编号已存在');
|
if (existing) throw new BadRequestException('该床位编号已存在');
|
||||||
const bed = this.bedRepo.create({ ...dto, roomId });
|
const bed = this.bedRepo.create({ ...dto, roomId });
|
||||||
@@ -377,7 +391,8 @@ export class RoomsService {
|
|||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
||||||
const existing = await this.bedRepo.find({ where: { roomId }, order: { bedNumber: 'ASC' } });
|
const existing = await this.bedRepo.find({ where: { roomId }, order: { bedNumber: 'ASC' } });
|
||||||
const numbers = existing.map(b => {
|
this.assertCanAddBedsFromCount(room, existing.length, dto.count);
|
||||||
|
const numbers = existing.map((b) => {
|
||||||
const match = b.bedNumber.match(/^\d+/);
|
const match = b.bedNumber.match(/^\d+/);
|
||||||
return match ? parseInt(match[0]) : 0;
|
return match ? parseInt(match[0]) : 0;
|
||||||
});
|
});
|
||||||
@@ -389,6 +404,27 @@ export class RoomsService {
|
|||||||
return this.bedRepo.save(beds);
|
return this.bedRepo.save(beds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async createDefaultBeds(roomId: number, capacity: number): Promise<void> {
|
||||||
|
const count = Math.max(capacity ?? 0, 0);
|
||||||
|
if (count === 0) return;
|
||||||
|
const beds = Array.from({ length: count }, (_, index) =>
|
||||||
|
this.bedRepo.create({ roomId, bedNumber: `${index + 1}号床` }),
|
||||||
|
);
|
||||||
|
await this.bedRepo.save(beds);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async assertCanAddBeds(room: Room, count: number): Promise<void> {
|
||||||
|
const existingCount = await this.bedRepo.count({ where: { roomId: room.id } });
|
||||||
|
this.assertCanAddBedsFromCount(room, existingCount, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private assertCanAddBedsFromCount(room: Room, existingCount: number, count: number): void {
|
||||||
|
const remaining = Math.max((room.capacity ?? 0) - existingCount, 0);
|
||||||
|
if (count > remaining) {
|
||||||
|
throw new BadRequestException(`床位不能超过额定人数,当前已有 ${existingCount} 张,额定 ${room.capacity} 张,最多还能添加 ${remaining} 张`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── 柜子管理 ──
|
// ── 柜子管理 ──
|
||||||
|
|
||||||
async getRoomLockers(roomId: number): Promise<Locker[]> {
|
async getRoomLockers(roomId: number): Promise<Locker[]> {
|
||||||
@@ -400,14 +436,19 @@ export class RoomsService {
|
|||||||
async getRoomAvailableLockers(roomId: number): Promise<Locker[]> {
|
async getRoomAvailableLockers(roomId: number): Promise<Locker[]> {
|
||||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
return this.lockerRepo.find({ where: { roomId, status: 'available' }, order: { lockerNumber: 'ASC' } });
|
return this.lockerRepo.find({
|
||||||
|
where: { roomId, status: 'available' },
|
||||||
|
order: { lockerNumber: 'ASC' },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createLocker(roomId: number, dto: CreateLockerDto): Promise<Locker> {
|
async createLocker(roomId: number, dto: CreateLockerDto): Promise<Locker> {
|
||||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
||||||
const existing = await this.lockerRepo.findOne({ where: { roomId, lockerNumber: dto.lockerNumber } });
|
const existing = await this.lockerRepo.findOne({
|
||||||
|
where: { roomId, lockerNumber: dto.lockerNumber },
|
||||||
|
});
|
||||||
if (existing) throw new BadRequestException('该柜子编号已存在');
|
if (existing) throw new BadRequestException('该柜子编号已存在');
|
||||||
const locker = this.lockerRepo.create({ ...dto, roomId });
|
const locker = this.lockerRepo.create({ ...dto, roomId });
|
||||||
return this.lockerRepo.save(locker);
|
return this.lockerRepo.save(locker);
|
||||||
@@ -420,7 +461,9 @@ export class RoomsService {
|
|||||||
throw new BadRequestException('该柜子有人占用,请先释放');
|
throw new BadRequestException('该柜子有人占用,请先释放');
|
||||||
}
|
}
|
||||||
if (dto.lockerNumber && dto.lockerNumber !== locker.lockerNumber) {
|
if (dto.lockerNumber && dto.lockerNumber !== locker.lockerNumber) {
|
||||||
const dup = await this.lockerRepo.findOne({ where: { roomId, lockerNumber: dto.lockerNumber } });
|
const dup = await this.lockerRepo.findOne({
|
||||||
|
where: { roomId, lockerNumber: dto.lockerNumber },
|
||||||
|
});
|
||||||
if (dup) throw new BadRequestException('该柜子编号已存在');
|
if (dup) throw new BadRequestException('该柜子编号已存在');
|
||||||
}
|
}
|
||||||
Object.assign(locker, dto);
|
Object.assign(locker, dto);
|
||||||
@@ -438,8 +481,11 @@ export class RoomsService {
|
|||||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||||
if (!room) throw new NotFoundException('宿舍不存在');
|
if (!room) throw new NotFoundException('宿舍不存在');
|
||||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
||||||
const existing = await this.lockerRepo.find({ where: { roomId }, order: { lockerNumber: 'ASC' } });
|
const existing = await this.lockerRepo.find({
|
||||||
const numbers = existing.map(b => {
|
where: { roomId },
|
||||||
|
order: { lockerNumber: 'ASC' },
|
||||||
|
});
|
||||||
|
const numbers = existing.map((b) => {
|
||||||
const match = b.lockerNumber.match(/^\d+/);
|
const match = b.lockerNumber.match(/^\d+/);
|
||||||
return match ? parseInt(match[0]) : 0;
|
return match ? parseInt(match[0]) : 0;
|
||||||
});
|
});
|
||||||
|
|||||||
47
apps/server/src/schedules/dto/schedule.dto.spec.ts
Normal file
47
apps/server/src/schedules/dto/schedule.dto.spec.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { validate } from 'class-validator';
|
||||||
|
import { CreateScheduleDto, UpdateScheduleDto } from './schedule.dto';
|
||||||
|
|
||||||
|
const createSchedule = (notes: string) =>
|
||||||
|
Object.assign(new CreateScheduleDto(), {
|
||||||
|
classId: 1,
|
||||||
|
classroomId: 2,
|
||||||
|
weekDay: 1,
|
||||||
|
startTime: '09:00',
|
||||||
|
endTime: '10:00',
|
||||||
|
startDate: '2026-07-01',
|
||||||
|
endDate: '2026-07-31',
|
||||||
|
subject: '语文',
|
||||||
|
notes,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('schedule notes validation', () => {
|
||||||
|
it('rejects notes longer than 500 characters when creating', async () => {
|
||||||
|
const errors = await validate(createSchedule('a'.repeat(501)));
|
||||||
|
expect(errors.some((error) => error.property === 'notes')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects notes longer than 500 characters when updating', async () => {
|
||||||
|
const dto = Object.assign(new UpdateScheduleDto(), { notes: 'a'.repeat(501) });
|
||||||
|
const errors = await validate(dto);
|
||||||
|
expect(errors.some((error) => error.property === 'notes')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes the retired departmentId field from create requests', async () => {
|
||||||
|
const dto = Object.assign(new CreateScheduleDto(), {
|
||||||
|
classId: 1,
|
||||||
|
classroomId: 2,
|
||||||
|
weekDay: 1,
|
||||||
|
startTime: '09:00',
|
||||||
|
endTime: '10:00',
|
||||||
|
startDate: '2026-07-01',
|
||||||
|
endDate: '2026-07-31',
|
||||||
|
subject: '语文',
|
||||||
|
departmentId: 99,
|
||||||
|
});
|
||||||
|
|
||||||
|
await validate(dto, { whitelist: true });
|
||||||
|
|
||||||
|
expect(dto).not.toHaveProperty('departmentId');
|
||||||
|
});
|
||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
Matches,
|
Matches,
|
||||||
Min,
|
Min,
|
||||||
Max,
|
Max,
|
||||||
|
MaxLength,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
@@ -59,11 +60,9 @@ export class CreateScheduleDto {
|
|||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@MaxLength(500)
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsInt()
|
|
||||||
departmentId?: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateScheduleDto {
|
export class UpdateScheduleDto {
|
||||||
@@ -115,6 +114,7 @@ export class UpdateScheduleDto {
|
|||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@MaxLength(500)
|
||||||
notes?: string;
|
notes?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
ClassSchedule,
|
ClassSchedule,
|
||||||
Class,
|
Class,
|
||||||
Classroom,
|
Classroom,
|
||||||
|
ClassroomStatus,
|
||||||
ClassroomRental,
|
ClassroomRental,
|
||||||
ClassTeacher,
|
ClassTeacher,
|
||||||
AttendanceSession,
|
AttendanceSession,
|
||||||
@@ -83,7 +84,7 @@ export class SchedulesService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const classrooms = await this.classroomRepo.find({
|
const classrooms = await this.classroomRepo.find({
|
||||||
where: { status: Not('archived') },
|
where: { status: ClassroomStatus.AVAILABLE },
|
||||||
select: ['id', 'name', 'building', 'floor', 'roomType'],
|
select: ['id', 'name', 'building', 'floor', 'roomType'],
|
||||||
order: { building: 'ASC', name: 'ASC' },
|
order: { building: 'ASC', name: 'ASC' },
|
||||||
});
|
});
|
||||||
@@ -159,7 +160,16 @@ export class SchedulesService {
|
|||||||
return schedule;
|
return schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async assertClassroomAvailable(classroomId: number) {
|
||||||
|
const classroom = await this.classroomRepo.findOne({ where: { id: classroomId } });
|
||||||
|
if (!classroom) throw new NotFoundException('教室不存在');
|
||||||
|
if (classroom.status !== ClassroomStatus.AVAILABLE) {
|
||||||
|
throw new BadRequestException('仅可用教室可以排课');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async create(dto: CreateScheduleDto) {
|
async create(dto: CreateScheduleDto) {
|
||||||
|
await this.assertClassroomAvailable(dto.classroomId);
|
||||||
await this.normalizeTeacherForSchedule(dto);
|
await this.normalizeTeacherForSchedule(dto);
|
||||||
await this.assertTeacherAssignedToClass(dto.classId, dto.teacherId);
|
await this.assertTeacherAssignedToClass(dto.classId, dto.teacherId);
|
||||||
await this.checkConflict(
|
await this.checkConflict(
|
||||||
@@ -182,6 +192,9 @@ export class SchedulesService {
|
|||||||
|
|
||||||
// If classroom, weekDay, or times are changing, check conflicts excluding self
|
// If classroom, weekDay, or times are changing, check conflicts excluding self
|
||||||
const classroomId = dto.classroomId ?? existing.classroomId;
|
const classroomId = dto.classroomId ?? existing.classroomId;
|
||||||
|
if (dto.classroomId !== undefined && dto.classroomId !== existing.classroomId) {
|
||||||
|
await this.assertClassroomAvailable(dto.classroomId);
|
||||||
|
}
|
||||||
const weekDay = dto.weekDay ?? existing.weekDay;
|
const weekDay = dto.weekDay ?? existing.weekDay;
|
||||||
const startTime = dto.startTime ?? existing.startTime;
|
const startTime = dto.startTime ?? existing.startTime;
|
||||||
const endTime = dto.endTime ?? existing.endTime;
|
const endTime = dto.endTime ?? existing.endTime;
|
||||||
@@ -253,7 +266,7 @@ export class SchedulesService {
|
|||||||
const rentalConflicts = await this.rentalRepo
|
const rentalConflicts = await this.rentalRepo
|
||||||
.createQueryBuilder('r')
|
.createQueryBuilder('r')
|
||||||
.where('r.classroomId = :classroomId', { classroomId })
|
.where('r.classroomId = :classroomId', { classroomId })
|
||||||
.andWhere('r.status != :cancelled', { cancelled: 'cancelled' })
|
.andWhere('r.status = :activeRental', { activeRental: 'active' })
|
||||||
.andWhere('r.startDate <= :endDate', { endDate })
|
.andWhere('r.startDate <= :endDate', { endDate })
|
||||||
.andWhere('r.endDate >= :startDate', { startDate })
|
.andWhere('r.endDate >= :startDate', { startDate })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
|||||||
18
apps/server/src/students/dto/student.dto.spec.ts
Normal file
18
apps/server/src/students/dto/student.dto.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { validate } from 'class-validator';
|
||||||
|
import { CreateStudentDto } from './student.dto';
|
||||||
|
|
||||||
|
describe('CreateStudentDto relationship boundaries', () => {
|
||||||
|
it('removes classId because class membership is managed by the classes API', async () => {
|
||||||
|
const dto = Object.assign(new CreateStudentDto(), {
|
||||||
|
name: '测试学生',
|
||||||
|
organizationId: 1,
|
||||||
|
classId: 9,
|
||||||
|
});
|
||||||
|
|
||||||
|
await validate(dto, { whitelist: true });
|
||||||
|
|
||||||
|
expect(dto).toMatchObject({ name: '测试学生', organizationId: 1 });
|
||||||
|
expect(dto).not.toHaveProperty('classId');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -38,9 +38,6 @@ export class CreateStudentDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
supervisor?: string;
|
supervisor?: string;
|
||||||
@IsOptional()
|
|
||||||
@IsInt()
|
|
||||||
classId?: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateStudentDto {
|
export class UpdateStudentDto {
|
||||||
|
|||||||
47
package-lock.json
generated
47
package-lock.json
generated
@@ -62,6 +62,7 @@
|
|||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.0.1",
|
||||||
"@nestjs/event-emitter": "^3.1.0",
|
"@nestjs/event-emitter": "^3.1.0",
|
||||||
"@nestjs/jwt": "^11.0.2",
|
"@nestjs/jwt": "^11.0.2",
|
||||||
|
"@nestjs/mapped-types": "^2.1.1",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/passport": "^11.0.5",
|
||||||
"@nestjs/platform-express": "^11.1.19",
|
"@nestjs/platform-express": "^11.1.19",
|
||||||
"@nestjs/schedule": "^6.1.3",
|
"@nestjs/schedule": "^6.1.3",
|
||||||
@@ -98,6 +99,7 @@
|
|||||||
"@types/passport-jwt": "^4.0.1",
|
"@types/passport-jwt": "^4.0.1",
|
||||||
"@types/passport-local": "^1.0.38",
|
"@types/passport-local": "^1.0.38",
|
||||||
"@types/supertest": "^7.0.0",
|
"@types/supertest": "^7.0.0",
|
||||||
|
"cross-env": "^10.1.0",
|
||||||
"eslint": "^9.18.0",
|
"eslint": "^9.18.0",
|
||||||
"globals": "^17.0.0",
|
"globals": "^17.0.0",
|
||||||
"jest": "^30.0.0",
|
"jest": "^30.0.0",
|
||||||
@@ -1133,6 +1135,13 @@
|
|||||||
"integrity": "sha512-OWORNpfjMsSSUBVrRBVGECkhWcULOAJz9ZW8uK9qgxD+87M7jHRcvh/A96XXNhXTLmKcoYSQtBEX7lHMO7YRwg==",
|
"integrity": "sha512-OWORNpfjMsSSUBVrRBVGECkhWcULOAJz9ZW8uK9qgxD+87M7jHRcvh/A96XXNhXTLmKcoYSQtBEX7lHMO7YRwg==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/@epic-web/invariant": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmmirror.com/@epic-web/invariant/-/invariant-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@eslint-community/eslint-utils": {
|
"node_modules/@eslint-community/eslint-utils": {
|
||||||
"version": "4.9.1",
|
"version": "4.9.1",
|
||||||
"resolved": "https://registry.npmmirror.com/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
|
"resolved": "https://registry.npmmirror.com/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
|
||||||
@@ -3093,6 +3102,26 @@
|
|||||||
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0"
|
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@nestjs/mapped-types": {
|
||||||
|
"version": "2.1.1",
|
||||||
|
"resolved": "https://registry.npmmirror.com/@nestjs/mapped-types/-/mapped-types-2.1.1.tgz",
|
||||||
|
"integrity": "sha512-SCCoMEJ6jdeI5h/N+KCVF1+pmg/hmEkNA5nHTS8Gvww7T/LCl4o1gFLinw2iQ60w7slFkszHcGLKGdazVI4F8A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
||||||
|
"class-transformer": "^0.4.0 || ^0.5.0",
|
||||||
|
"class-validator": "^0.13.0 || ^0.14.0 || ^0.15.0",
|
||||||
|
"reflect-metadata": "^0.1.12 || ^0.2.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"class-transformer": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"class-validator": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@nestjs/passport": {
|
"node_modules/@nestjs/passport": {
|
||||||
"version": "11.0.5",
|
"version": "11.0.5",
|
||||||
"resolved": "https://registry.npmmirror.com/@nestjs/passport/-/passport-11.0.5.tgz",
|
"resolved": "https://registry.npmmirror.com/@nestjs/passport/-/passport-11.0.5.tgz",
|
||||||
@@ -8697,6 +8726,24 @@
|
|||||||
"url": "https://ko-fi.com/intcreator"
|
"url": "https://ko-fi.com/intcreator"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/cross-env": {
|
||||||
|
"version": "10.1.0",
|
||||||
|
"resolved": "https://registry.npmmirror.com/cross-env/-/cross-env-10.1.0.tgz",
|
||||||
|
"integrity": "sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@epic-web/invariant": "^1.0.0",
|
||||||
|
"cross-spawn": "^7.0.6"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"cross-env": "dist/bin/cross-env.js",
|
||||||
|
"cross-env-shell": "dist/bin/cross-env-shell.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.6",
|
"version": "7.0.6",
|
||||||
"resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
"resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||||
|
|||||||
Reference in New Issue
Block a user