feat(M08-B): 增加保洁多人协作策略
This commit is contained in:
@@ -40,9 +40,21 @@ interface CleaningTaskRow extends RowDataPacket {
|
||||
startedAt: Date | null;
|
||||
submittedAt: Date | null;
|
||||
completedAt: Date | null;
|
||||
memberCount: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
interface CleaningTaskMemberRow extends RowDataPacket {
|
||||
id: string;
|
||||
taskId: string;
|
||||
userId: string;
|
||||
nickname: string;
|
||||
memberRole: 'LEAD' | 'ASSIST';
|
||||
rewardCents: number;
|
||||
joinedAt: Date;
|
||||
removedAt: Date | null;
|
||||
settledAt: Date | null;
|
||||
}
|
||||
interface CountRow extends RowDataPacket { total: number }
|
||||
interface StatusCountRow extends RowDataPacket { status: CleaningTaskStatus; total: number }
|
||||
interface AmountRow extends RowDataPacket { amount: number | null }
|
||||
@@ -99,9 +111,13 @@ export class CleaningTaskRepository {
|
||||
const where = [
|
||||
't.tenant_id = ?',
|
||||
't.deleted_at IS NULL',
|
||||
't.cleaner_user_id = ?'
|
||||
`(t.cleaner_user_id = ? OR EXISTS (
|
||||
SELECT 1 FROM qipai_cleaning_task_members mm
|
||||
WHERE mm.tenant_id = t.tenant_id AND mm.task_id = t.id
|
||||
AND mm.user_id = ? AND mm.removed_at IS NULL
|
||||
))`
|
||||
];
|
||||
const params: Array<string | number> = [input.tenantId, input.userId];
|
||||
const params: Array<string | number> = [input.tenantId, input.userId, input.userId];
|
||||
if (input.status) {
|
||||
where.push('t.status = ?');
|
||||
params.push(input.status);
|
||||
@@ -135,6 +151,7 @@ export class CleaningTaskRepository {
|
||||
[input.userId, input.tenantId, input.taskId]
|
||||
);
|
||||
if (result.affectedRows !== 1) throw new CleaningTaskError('CLEANING_TASK_NOT_CLAIMABLE');
|
||||
await this.ensureLeadMember(this.pool, input.tenantId, input.taskId);
|
||||
await this.recordEvent(input, input.taskId, 'WAITING', 'CLAIMED', 'CLAIM', '');
|
||||
return this.getMineTask(input, input.taskId);
|
||||
}
|
||||
@@ -163,12 +180,15 @@ export class CleaningTaskRepository {
|
||||
[input.cleanerUserId, input.tenantId, input.taskId]
|
||||
);
|
||||
if (result.affectedRows !== 1) throw new CleaningTaskError('CLEANING_TASK_ASSIGN_CONFLICT');
|
||||
await this.ensureLeadMember(this.pool, input.tenantId, input.taskId);
|
||||
await this.recordEvent(input, input.taskId, 'WAITING', 'CLAIMED', 'ASSIGN', input.note ?? '');
|
||||
return this.getTask(input, input.taskId);
|
||||
}
|
||||
|
||||
async complete(input: CleaningActor & { taskId: string; note?: string }) {
|
||||
return this.moveManaged(input, 'SUBMITTED', 'COMPLETED', 'COMPLETE', 'completed_at', input.note ?? '');
|
||||
const task = await this.moveManaged(input, 'SUBMITTED', 'COMPLETED', 'COMPLETE', 'completed_at', input.note ?? '');
|
||||
await this.ensureLeadMember(this.pool, input.tenantId, input.taskId);
|
||||
return task;
|
||||
}
|
||||
|
||||
async reject(input: CleaningActor & { taskId: string; reason: string }) {
|
||||
@@ -185,6 +205,105 @@ export class CleaningTaskRepository {
|
||||
return this.getTask(input, input.taskId);
|
||||
}
|
||||
|
||||
async listMembers(input: CleaningActor & { taskId: string }) {
|
||||
this.assertCleaner(input.access, 'read');
|
||||
await this.assertStoreVisible(input, input.taskId);
|
||||
return this.getMembers(input.tenantId, input.taskId);
|
||||
}
|
||||
|
||||
async addMember(input: CleaningActor & {
|
||||
taskId: string; cleanerUserId: string; rewardCents: number; note?: string;
|
||||
}) {
|
||||
this.assertCleaner(input.access, 'write');
|
||||
await this.assertStoreVisible(input, input.taskId);
|
||||
await this.assertCleanerUserForTask(input.tenantId, input.taskId, input.cleanerUserId);
|
||||
return this.transaction(async (connection) => {
|
||||
const [tasks] = await connection.execute<CleaningTaskRow[]>(
|
||||
`SELECT t.id, t.task_no AS taskNo, t.store_id AS storeId, '' AS storeName,
|
||||
t.room_id AS roomId, '' AS roomName, '' AS roomNo, t.order_id AS orderId,
|
||||
NULL AS orderNo, t.status, t.cleaner_user_id AS cleanerUserId,
|
||||
t.priority, t.reward_cents AS rewardCents, t.requirement,
|
||||
t.photo_urls_json AS photoUrlsJson, t.reject_reason AS rejectReason,
|
||||
t.claimed_at AS claimedAt, t.started_at AS startedAt,
|
||||
t.submitted_at AS submittedAt, t.completed_at AS completedAt,
|
||||
0 AS memberCount, t.created_at AS createdAt, t.updated_at AS updatedAt
|
||||
FROM qipai_cleaning_tasks t
|
||||
WHERE t.tenant_id = ? AND t.id = ? AND t.deleted_at IS NULL
|
||||
FOR UPDATE`,
|
||||
[input.tenantId, input.taskId]
|
||||
);
|
||||
const task = tasks[0];
|
||||
if (!task) throw new CleaningTaskError('CLEANING_TASK_NOT_FOUND');
|
||||
if (!task.cleanerUserId) throw new CleaningTaskError('CLEANING_TASK_ASSIGNEE_REQUIRED');
|
||||
if (String(task.cleanerUserId) === input.cleanerUserId) {
|
||||
throw new CleaningTaskError('CLEANING_MEMBER_DUPLICATE_LEAD');
|
||||
}
|
||||
if (!['CLAIMED', 'STARTED', 'SUBMITTED', 'COMPLETED'].includes(task.status)) {
|
||||
throw new CleaningTaskError('CLEANING_TASK_STATUS_CONFLICT');
|
||||
}
|
||||
const [existingMembers] = await connection.execute<CleaningTaskMemberRow[]>(
|
||||
`SELECT m.id, m.task_id AS taskId, m.user_id AS userId, '' AS nickname,
|
||||
m.member_role AS memberRole, m.reward_cents AS rewardCents,
|
||||
m.joined_at AS joinedAt, m.removed_at AS removedAt, m.settled_at AS settledAt
|
||||
FROM qipai_cleaning_task_members m
|
||||
WHERE m.tenant_id = ? AND m.task_id = ? AND m.user_id = ?
|
||||
FOR UPDATE`,
|
||||
[input.tenantId, input.taskId, input.cleanerUserId]
|
||||
);
|
||||
if (existingMembers[0]?.settledAt) throw new CleaningTaskError('CLEANING_MEMBER_SETTLED');
|
||||
await this.ensureLeadMember(connection, input.tenantId, input.taskId);
|
||||
await connection.execute(
|
||||
`INSERT INTO qipai_cleaning_task_members
|
||||
(tenant_id, task_id, user_id, member_role, reward_cents, joined_at, removed_at, settled_at)
|
||||
VALUES (?, ?, ?, 'ASSIST', ?, UTC_TIMESTAMP(3), NULL, NULL)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
member_role = 'ASSIST',
|
||||
reward_cents = VALUES(reward_cents),
|
||||
removed_at = NULL,
|
||||
settled_at = NULL,
|
||||
updated_at = UTC_TIMESTAMP(3)`,
|
||||
[input.tenantId, input.taskId, input.cleanerUserId, input.rewardCents]
|
||||
);
|
||||
await this.syncLeadReward(connection, input.tenantId, input.taskId);
|
||||
await this.recordEventWithConnection(
|
||||
connection, input, input.taskId, task.status, task.status, 'ADD_MEMBER', input.note ?? ''
|
||||
);
|
||||
return this.getMembers(input.tenantId, input.taskId, connection);
|
||||
});
|
||||
}
|
||||
|
||||
async removeMember(input: CleaningActor & { taskId: string; cleanerUserId: string; note?: string }) {
|
||||
this.assertCleaner(input.access, 'write');
|
||||
await this.assertStoreVisible(input, input.taskId);
|
||||
return this.transaction(async (connection) => {
|
||||
const [tasks] = await connection.execute<CurrentStatusRow[]>(
|
||||
`SELECT status, cleaner_user_id AS cleanerUserId
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND id = ? AND deleted_at IS NULL
|
||||
FOR UPDATE`,
|
||||
[input.tenantId, input.taskId]
|
||||
);
|
||||
const task = tasks[0];
|
||||
if (!task) throw new CleaningTaskError('CLEANING_TASK_NOT_FOUND');
|
||||
if (String(task.cleanerUserId) === input.cleanerUserId) {
|
||||
throw new CleaningTaskError('CLEANING_MEMBER_LEAD_NOT_REMOVABLE');
|
||||
}
|
||||
const [result] = await connection.execute<ResultSetHeader>(
|
||||
`UPDATE qipai_cleaning_task_members
|
||||
SET removed_at = UTC_TIMESTAMP(3), reward_cents = 0, updated_at = UTC_TIMESTAMP(3)
|
||||
WHERE tenant_id = ? AND task_id = ? AND user_id = ?
|
||||
AND member_role = 'ASSIST' AND removed_at IS NULL AND settled_at IS NULL`,
|
||||
[input.tenantId, input.taskId, input.cleanerUserId]
|
||||
);
|
||||
if (result.affectedRows !== 1) throw new CleaningTaskError('CLEANING_MEMBER_NOT_FOUND');
|
||||
await this.syncLeadReward(connection, input.tenantId, input.taskId);
|
||||
await this.recordEventWithConnection(
|
||||
connection, input, input.taskId, task.status, task.status, 'REMOVE_MEMBER', input.note ?? ''
|
||||
);
|
||||
return this.getMembers(input.tenantId, input.taskId, connection);
|
||||
});
|
||||
}
|
||||
|
||||
async settlementCandidates(input: CleaningActor & { page: number; pageSize: number }) {
|
||||
this.assertCleaner(input.access, 'read');
|
||||
const where = [
|
||||
@@ -246,20 +365,34 @@ export class CleaningTaskRepository {
|
||||
}) {
|
||||
this.assertSettlement(input.access, 'write');
|
||||
return this.transaction(async (connection) => {
|
||||
await connection.execute(
|
||||
`INSERT IGNORE INTO qipai_cleaning_task_members
|
||||
(tenant_id, task_id, user_id, member_role, reward_cents, joined_at)
|
||||
SELECT t.tenant_id, t.id, t.cleaner_user_id, 'LEAD', t.reward_cents,
|
||||
COALESCE(t.claimed_at, t.completed_at, UTC_TIMESTAMP(3))
|
||||
FROM qipai_cleaning_tasks t
|
||||
WHERE t.tenant_id = ? AND t.cleaner_user_id = ?
|
||||
AND t.status = 'COMPLETED' AND t.deleted_at IS NULL
|
||||
AND t.cleaner_user_id IS NOT NULL`,
|
||||
[input.tenantId, input.cleanerUserId]
|
||||
);
|
||||
const scopeSql = storeScopeSql(input.access, 't.store_id');
|
||||
const params: Array<string | number> = [input.tenantId, input.cleanerUserId];
|
||||
const storeFilter = input.storeId ? 'AND t.store_id = ?' : '';
|
||||
if (input.storeId) params.push(input.storeId);
|
||||
const [tasks] = await connection.execute<SettlementCandidateRow[]>(
|
||||
`SELECT t.id, t.store_id AS storeId, t.cleaner_user_id AS cleanerUserId,
|
||||
t.reward_cents AS rewardCents, t.completed_at AS completedAt
|
||||
`SELECT t.id, t.store_id AS storeId, m.user_id AS cleanerUserId,
|
||||
m.reward_cents AS rewardCents, t.completed_at AS completedAt
|
||||
FROM qipai_cleaning_tasks t
|
||||
WHERE t.tenant_id = ? AND t.cleaner_user_id = ?
|
||||
AND t.status = 'COMPLETED' AND t.settled_at IS NULL AND t.deleted_at IS NULL
|
||||
INNER JOIN qipai_cleaning_task_members m ON m.tenant_id = t.tenant_id
|
||||
AND m.task_id = t.id AND m.user_id = ? AND m.removed_at IS NULL
|
||||
AND m.settled_at IS NULL AND m.reward_cents > 0
|
||||
WHERE t.tenant_id = ?
|
||||
AND t.status = 'COMPLETED' AND t.deleted_at IS NULL
|
||||
${storeFilter} AND ${scopeSql}
|
||||
ORDER BY t.completed_at ASC, t.id ASC
|
||||
FOR UPDATE`,
|
||||
params
|
||||
[input.cleanerUserId, input.tenantId, ...params.slice(2)]
|
||||
);
|
||||
if (tasks.length === 0) throw new CleaningTaskError('CLEANING_SETTLEMENT_EMPTY');
|
||||
const storeIds = Array.from(new Set(tasks.map((task) => String(task.storeId))));
|
||||
@@ -283,20 +416,32 @@ export class CleaningTaskRepository {
|
||||
for (const task of tasks) {
|
||||
await connection.execute(
|
||||
`INSERT INTO qipai_cleaning_settlement_items
|
||||
(tenant_id, settlement_id, task_id, reward_cents)
|
||||
VALUES (?, ?, ?, ?)`,
|
||||
[input.tenantId, settlementId, task.id, Number(task.rewardCents)]
|
||||
(tenant_id, settlement_id, task_id, cleaner_user_id, reward_cents)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[input.tenantId, settlementId, task.id, input.cleanerUserId, Number(task.rewardCents)]
|
||||
);
|
||||
}
|
||||
await connection.execute(
|
||||
`UPDATE qipai_cleaning_tasks
|
||||
SET status = 'SETTLED', settled_at = UTC_TIMESTAMP(3)
|
||||
WHERE tenant_id = ? AND id IN (${tasks.map(() => '?').join(',')})`,
|
||||
`UPDATE qipai_cleaning_task_members
|
||||
SET settled_at = UTC_TIMESTAMP(3)
|
||||
WHERE tenant_id = ? AND user_id = ? AND task_id IN (${tasks.map(() => '?').join(',')})`,
|
||||
[input.tenantId, input.cleanerUserId, ...tasks.map((task) => task.id)]
|
||||
);
|
||||
await connection.execute(
|
||||
`UPDATE qipai_cleaning_tasks t
|
||||
SET t.status = 'SETTLED', t.settled_at = UTC_TIMESTAMP(3)
|
||||
WHERE t.tenant_id = ? AND t.id IN (${tasks.map(() => '?').join(',')})
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM qipai_cleaning_task_members pending
|
||||
WHERE pending.tenant_id = t.tenant_id AND pending.task_id = t.id
|
||||
AND pending.removed_at IS NULL AND pending.reward_cents > 0
|
||||
AND pending.settled_at IS NULL
|
||||
)`,
|
||||
[input.tenantId, ...tasks.map((task) => task.id)]
|
||||
);
|
||||
for (const task of tasks) {
|
||||
await this.recordEventWithConnection(
|
||||
connection, input, String(task.id), 'COMPLETED', 'SETTLED', 'SETTLE', settlementNo
|
||||
connection, input, String(task.id), 'COMPLETED', 'COMPLETED', 'SETTLE_MEMBER', settlementNo
|
||||
);
|
||||
}
|
||||
return this.getSettlement(connection, input.tenantId, settlementId);
|
||||
@@ -422,16 +567,23 @@ export class CleaningTaskRepository {
|
||||
this.assertCleaner(input.access, 'read');
|
||||
const [counts] = await this.pool.execute<StatusCountRow[]>(
|
||||
`SELECT status, COUNT(*) AS total
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND cleaner_user_id = ? AND deleted_at IS NULL
|
||||
FROM qipai_cleaning_tasks t
|
||||
WHERE tenant_id = ? AND deleted_at IS NULL
|
||||
AND (cleaner_user_id = ? OR EXISTS (
|
||||
SELECT 1 FROM qipai_cleaning_task_members m
|
||||
WHERE m.tenant_id = t.tenant_id AND m.task_id = t.id
|
||||
AND m.user_id = ? AND m.removed_at IS NULL
|
||||
))
|
||||
GROUP BY status`,
|
||||
[input.tenantId, input.userId]
|
||||
[input.tenantId, input.userId, input.userId]
|
||||
);
|
||||
const [amountRows] = await this.pool.execute<AmountRow[]>(
|
||||
`SELECT COALESCE(SUM(reward_cents), 0) AS amount
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND cleaner_user_id = ? AND status IN ('SUBMITTED', 'COMPLETED')
|
||||
AND settled_at IS NULL AND deleted_at IS NULL`,
|
||||
`SELECT COALESCE(SUM(m.reward_cents), 0) AS amount
|
||||
FROM qipai_cleaning_task_members m
|
||||
INNER JOIN qipai_cleaning_tasks t ON t.tenant_id = m.tenant_id AND t.id = m.task_id
|
||||
WHERE m.tenant_id = ? AND m.user_id = ? AND m.removed_at IS NULL
|
||||
AND m.settled_at IS NULL AND m.reward_cents > 0
|
||||
AND t.status IN ('SUBMITTED', 'COMPLETED') AND t.deleted_at IS NULL`,
|
||||
[input.tenantId, input.userId]
|
||||
);
|
||||
return {
|
||||
@@ -461,6 +613,9 @@ export class CleaningTaskRepository {
|
||||
t.requirement, t.photo_urls_json AS photoUrlsJson, t.reject_reason AS rejectReason,
|
||||
t.claimed_at AS claimedAt, t.started_at AS startedAt,
|
||||
t.submitted_at AS submittedAt, t.completed_at AS completedAt,
|
||||
(SELECT COUNT(*) FROM qipai_cleaning_task_members m
|
||||
WHERE m.tenant_id = t.tenant_id AND m.task_id = t.id
|
||||
AND m.removed_at IS NULL) AS memberCount,
|
||||
t.created_at AS createdAt, t.updated_at AS updatedAt
|
||||
FROM qipai_cleaning_tasks t
|
||||
INNER JOIN qipai_stores s ON s.tenant_id = t.tenant_id AND s.id = t.store_id
|
||||
@@ -542,6 +697,81 @@ export class CleaningTaskRepository {
|
||||
return this.getMineTask(input, input.taskId);
|
||||
}
|
||||
|
||||
private async getMembers(
|
||||
tenantId: string,
|
||||
taskId: string,
|
||||
connection: Pick<MySqlPool, 'execute'> | PoolConnection = this.pool
|
||||
) {
|
||||
const [rows] = await connection.execute<CleaningTaskMemberRow[]>(
|
||||
`SELECT m.id, m.task_id AS taskId, m.user_id AS userId,
|
||||
u.nickname, m.member_role AS memberRole, m.reward_cents AS rewardCents,
|
||||
m.joined_at AS joinedAt, m.removed_at AS removedAt, m.settled_at AS settledAt
|
||||
FROM qipai_cleaning_task_members m
|
||||
INNER JOIN qipai_users u ON u.tenant_id = m.tenant_id AND u.id = m.user_id
|
||||
WHERE m.tenant_id = ? AND m.task_id = ? AND m.removed_at IS NULL
|
||||
ORDER BY FIELD(m.member_role, 'LEAD', 'ASSIST'), m.joined_at ASC, m.id ASC`,
|
||||
[tenantId, taskId]
|
||||
);
|
||||
return rows.map(publicTaskMember);
|
||||
}
|
||||
|
||||
private async ensureLeadMember(
|
||||
connection: Pick<MySqlPool, 'execute'> | PoolConnection,
|
||||
tenantId: string,
|
||||
taskId: string
|
||||
) {
|
||||
await connection.execute(
|
||||
`UPDATE qipai_cleaning_task_members m
|
||||
INNER JOIN qipai_cleaning_tasks t ON t.tenant_id = m.tenant_id AND t.id = m.task_id
|
||||
SET m.removed_at = UTC_TIMESTAMP(3), m.reward_cents = 0, m.updated_at = UTC_TIMESTAMP(3)
|
||||
WHERE m.tenant_id = ? AND m.task_id = ? AND m.member_role = 'LEAD'
|
||||
AND m.user_id <> t.cleaner_user_id AND m.removed_at IS NULL AND m.settled_at IS NULL`,
|
||||
[tenantId, taskId]
|
||||
);
|
||||
await connection.execute(
|
||||
`INSERT INTO qipai_cleaning_task_members
|
||||
(tenant_id, task_id, user_id, member_role, reward_cents, joined_at)
|
||||
SELECT t.tenant_id, t.id, t.cleaner_user_id, 'LEAD', t.reward_cents,
|
||||
COALESCE(t.claimed_at, UTC_TIMESTAMP(3))
|
||||
FROM qipai_cleaning_tasks t
|
||||
WHERE t.tenant_id = ? AND t.id = ? AND t.cleaner_user_id IS NOT NULL
|
||||
ON DUPLICATE KEY UPDATE
|
||||
member_role = 'LEAD',
|
||||
removed_at = NULL,
|
||||
updated_at = UTC_TIMESTAMP(3)`,
|
||||
[tenantId, taskId]
|
||||
);
|
||||
await this.syncLeadReward(connection, tenantId, taskId);
|
||||
}
|
||||
|
||||
private async syncLeadReward(
|
||||
connection: Pick<MySqlPool, 'execute'> | PoolConnection,
|
||||
tenantId: string,
|
||||
taskId: string
|
||||
) {
|
||||
const [rows] = await connection.execute<AmountRow[]>(
|
||||
`SELECT t.reward_cents - COALESCE(SUM(
|
||||
CASE WHEN m.member_role = 'ASSIST' AND m.removed_at IS NULL THEN m.reward_cents ELSE 0 END
|
||||
), 0) AS amount
|
||||
FROM qipai_cleaning_tasks t
|
||||
LEFT JOIN qipai_cleaning_task_members m ON m.tenant_id = t.tenant_id AND m.task_id = t.id
|
||||
WHERE t.tenant_id = ? AND t.id = ?
|
||||
GROUP BY t.reward_cents`,
|
||||
[tenantId, taskId]
|
||||
);
|
||||
const leadReward = Number(rows[0]?.amount ?? 0);
|
||||
if (leadReward < 0) throw new CleaningTaskError('CLEANING_MEMBER_REWARD_EXCEEDS_TASK');
|
||||
await connection.execute(
|
||||
`UPDATE qipai_cleaning_task_members m
|
||||
INNER JOIN qipai_cleaning_tasks t ON t.tenant_id = m.tenant_id
|
||||
AND t.id = m.task_id AND t.cleaner_user_id = m.user_id
|
||||
SET m.reward_cents = ?, m.member_role = 'LEAD', m.removed_at = NULL,
|
||||
m.updated_at = UTC_TIMESTAMP(3)
|
||||
WHERE m.tenant_id = ? AND m.task_id = ?`,
|
||||
[leadReward, tenantId, taskId]
|
||||
);
|
||||
}
|
||||
|
||||
private async assertStoreVisible(input: CleaningActor, taskId: string) {
|
||||
if (input.access.capabilities.includes('tenant.manage') || input.access.roles.includes('PLATFORM_ADMIN')) return;
|
||||
const [rows] = await this.pool.execute<RowDataPacket[]>(
|
||||
@@ -694,11 +924,26 @@ function publicTask(row: CleaningTaskRow) {
|
||||
startedAt: row.startedAt,
|
||||
submittedAt: row.submittedAt,
|
||||
completedAt: row.completedAt,
|
||||
memberCount: Number(row.memberCount ?? 0),
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt
|
||||
};
|
||||
}
|
||||
|
||||
function publicTaskMember(row: CleaningTaskMemberRow) {
|
||||
return {
|
||||
id: String(row.id),
|
||||
taskId: String(row.taskId),
|
||||
userId: String(row.userId),
|
||||
nickname: row.nickname,
|
||||
memberRole: row.memberRole,
|
||||
rewardCents: Number(row.rewardCents),
|
||||
joinedAt: row.joinedAt,
|
||||
removedAt: row.removedAt,
|
||||
settledAt: row.settledAt
|
||||
};
|
||||
}
|
||||
|
||||
function publicSettlement(row: SettlementRow) {
|
||||
return {
|
||||
id: String(row.id),
|
||||
|
||||
@@ -46,7 +46,8 @@ const migrationFiles: Record<MigrationDirection, readonly string[]> = {
|
||||
'database/migrations/2026062423_m07c_benefits.up.sql',
|
||||
'database/migrations/2026062524_m08a_recharge_wechat.up.sql',
|
||||
'database/migrations/2026062525_m08b_cleaner_tasks.up.sql',
|
||||
'database/migrations/2026062626_m08b_cleaning_settlements.up.sql'
|
||||
'database/migrations/2026062626_m08b_cleaning_settlements.up.sql',
|
||||
'database/migrations/2026062627_m08b_cleaning_collaboration.up.sql'
|
||||
],
|
||||
verify: [
|
||||
'database/migrations/2026061601_m01b_core_schema.verify.sql',
|
||||
@@ -74,9 +75,11 @@ const migrationFiles: Record<MigrationDirection, readonly string[]> = {
|
||||
'database/migrations/2026062423_m07c_benefits.verify.sql',
|
||||
'database/migrations/2026062524_m08a_recharge_wechat.verify.sql',
|
||||
'database/migrations/2026062525_m08b_cleaner_tasks.verify.sql',
|
||||
'database/migrations/2026062626_m08b_cleaning_settlements.verify.sql'
|
||||
'database/migrations/2026062626_m08b_cleaning_settlements.verify.sql',
|
||||
'database/migrations/2026062627_m08b_cleaning_collaboration.verify.sql'
|
||||
],
|
||||
down: [
|
||||
'database/migrations/2026062627_m08b_cleaning_collaboration.down.sql',
|
||||
'database/migrations/2026062626_m08b_cleaning_settlements.down.sql',
|
||||
'database/migrations/2026062525_m08b_cleaner_tasks.down.sql',
|
||||
'database/migrations/2026062524_m08a_recharge_wechat.down.sql',
|
||||
@@ -243,7 +246,8 @@ export async function executeMigrationPlan(
|
||||
1, 1, 7, 7, 1,
|
||||
5, 10, 7, 3, 1,
|
||||
2, 8, 4, 3, 1,
|
||||
2, 9, 5, 2, 1
|
||||
2, 9, 5, 2, 1,
|
||||
1, 7, 5, 1
|
||||
][index] ?? 1;
|
||||
if (!Array.isArray(result) || result.length < minimumRows) {
|
||||
throw new Error(
|
||||
|
||||
@@ -25,6 +25,18 @@ const assignSchema = z.object({
|
||||
cleanerUserId: z.string().regex(/^[1-9]\d{0,19}$/),
|
||||
note: z.string().trim().max(512).optional()
|
||||
}).strict();
|
||||
const memberParamsSchema = z.object({
|
||||
taskId: z.string().regex(/^[1-9]\d{0,19}$/),
|
||||
cleanerUserId: z.string().regex(/^[1-9]\d{0,19}$/)
|
||||
});
|
||||
const memberSchema = z.object({
|
||||
cleanerUserId: z.string().regex(/^[1-9]\d{0,19}$/),
|
||||
rewardCents: z.coerce.number().int().min(0).max(1000000),
|
||||
note: z.string().trim().max(512).optional()
|
||||
}).strict();
|
||||
const memberRemoveSchema = z.object({
|
||||
note: z.string().trim().max(512).optional()
|
||||
}).strict();
|
||||
const completeSchema = z.object({ note: z.string().trim().max(512).optional() }).strict();
|
||||
const rejectSchema = z.object({ reason: z.string().trim().min(1).max(512) }).strict();
|
||||
const settlementStatusSchema = z.enum(['DRAFT', 'CONFIRMED', 'PAID', 'CANCELLED']);
|
||||
@@ -50,7 +62,7 @@ const reclaimSchema = z.object({
|
||||
export interface CleaningRouteOptions {
|
||||
repository: Pick<CleaningTaskRepository,
|
||||
'listHall' | 'listMine' | 'listManage' | 'claim' | 'start' | 'rework' | 'submit'
|
||||
| 'assign' | 'complete' | 'reject' | 'settlementCandidates'
|
||||
| 'assign' | 'complete' | 'reject' | 'listMembers' | 'addMember' | 'removeMember' | 'settlementCandidates'
|
||||
| 'listSettlements' | 'generateSettlement' | 'confirmSettlement' | 'reclaimTimeouts'
|
||||
| 'assertCanUploadPhoto' | 'stats'>;
|
||||
mediaStorage?: MediaStorage;
|
||||
@@ -171,6 +183,55 @@ export async function registerCleaningRoutes(
|
||||
}));
|
||||
});
|
||||
|
||||
app.get('/admin-api/cleaning/tasks/:taskId/members', async (request, reply) => {
|
||||
const actor = await requireActor(request, reply, options, 'read');
|
||||
if (!actor) return;
|
||||
const params = paramsSchema.safeParse(request.params);
|
||||
if (!params.success) return invalid(reply, request.traceId);
|
||||
return handle(reply, request.traceId, async () => ({
|
||||
code: 0,
|
||||
data: await options.repository.listMembers({ ...actor, taskId: params.data.taskId }),
|
||||
traceId: request.traceId
|
||||
}));
|
||||
});
|
||||
|
||||
app.post('/admin-api/cleaning/tasks/:taskId/members', async (request, reply) => {
|
||||
const actor = await requireActor(request, reply, options, 'write');
|
||||
if (!actor) return;
|
||||
const params = paramsSchema.safeParse(request.params);
|
||||
const body = memberSchema.safeParse(request.body ?? {});
|
||||
if (!params.success || !body.success) return invalid(reply, request.traceId);
|
||||
return handle(reply, request.traceId, async () => ({
|
||||
code: 0,
|
||||
data: await options.repository.addMember({
|
||||
...actor,
|
||||
taskId: params.data.taskId,
|
||||
cleanerUserId: body.data.cleanerUserId,
|
||||
rewardCents: body.data.rewardCents,
|
||||
note: body.data.note
|
||||
}),
|
||||
traceId: request.traceId
|
||||
}));
|
||||
});
|
||||
|
||||
app.post('/admin-api/cleaning/tasks/:taskId/members/:cleanerUserId/remove', async (request, reply) => {
|
||||
const actor = await requireActor(request, reply, options, 'write');
|
||||
if (!actor) return;
|
||||
const params = memberParamsSchema.safeParse(request.params);
|
||||
const body = memberRemoveSchema.safeParse(request.body ?? {});
|
||||
if (!params.success || !body.success) return invalid(reply, request.traceId);
|
||||
return handle(reply, request.traceId, async () => ({
|
||||
code: 0,
|
||||
data: await options.repository.removeMember({
|
||||
...actor,
|
||||
taskId: params.data.taskId,
|
||||
cleanerUserId: params.data.cleanerUserId,
|
||||
note: body.data.note
|
||||
}),
|
||||
traceId: request.traceId
|
||||
}));
|
||||
});
|
||||
|
||||
app.post('/app-api/cleaning/tasks/:taskId/start', async (request, reply) => {
|
||||
const actor = await requireActor(request, reply, options, 'write');
|
||||
if (!actor) return;
|
||||
|
||||
Reference in New Issue
Block a user