feat(purchase): 소모품 신청 시스템 v2 — 모바일 최적화, 스마트 검색, 그룹화, 입고 알림
- 4단계 상태 플로우: pending → grouped → purchased → received - 한국어 스마트 검색: 초성 매칭(ㅁㅈㄱ→면장갑), 별칭 테이블, 인메모리 캐시 - 모바일 전용 신청 페이지: 바텀시트 UI, FAB, 카드 리스트, 스크롤 페이지네이션 - 인라인 품목 등록: 미등록 품목 검색→등록→신청 단일 트랜잭션 - 관리자 그룹화: 체크박스 다중 선택, 구매 그룹(batch) 생성/일괄 구매/입고 - 입고 처리: 사진+보관위치 등록, 부분 입고 허용, batch 자동 상태 전환 - 알림: notifyHelper에 target_user_ids 추가, 구매진행중/입고완료 시 신청자 ntfy+push Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,17 +2,19 @@
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
const PurchaseRequestModel = {
|
||||
// 구매신청 목록 (소모품 정보 LEFT JOIN — item_id NULL 허용)
|
||||
// 구매신청 목록 (소모품 정보 LEFT JOIN — item_id NULL 허용, batch 정보 포함)
|
||||
async getAll(filters = {}) {
|
||||
const db = await getDb();
|
||||
let sql = `
|
||||
SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
|
||||
ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
|
||||
pr.custom_item_name, pr.custom_category,
|
||||
su.name AS requester_name
|
||||
su.name AS requester_name,
|
||||
pb.batch_name, pb.status AS batch_status, pb.category AS batch_category
|
||||
FROM purchase_requests pr
|
||||
LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
|
||||
LEFT JOIN sso_users su ON pr.requester_id = su.user_id
|
||||
LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
|
||||
WHERE 1=1
|
||||
`;
|
||||
const params = [];
|
||||
@@ -25,23 +27,28 @@ const PurchaseRequestModel = {
|
||||
}
|
||||
if (filters.from_date) { sql += ' AND pr.request_date >= ?'; params.push(filters.from_date); }
|
||||
if (filters.to_date) { sql += ' AND pr.request_date <= ?'; params.push(filters.to_date); }
|
||||
if (filters.batch_id) { sql += ' AND pr.batch_id = ?'; params.push(filters.batch_id); }
|
||||
|
||||
sql += ' ORDER BY pr.created_at DESC';
|
||||
const [rows] = await db.query(sql, params);
|
||||
return rows;
|
||||
},
|
||||
|
||||
// 단건 조회
|
||||
// 단건 조회 (batch 정보 포함)
|
||||
async getById(requestId) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(`
|
||||
SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
|
||||
ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
|
||||
pr.custom_item_name, pr.custom_category,
|
||||
su.name AS requester_name
|
||||
su.name AS requester_name,
|
||||
pb.batch_name, pb.status AS batch_status, pb.category AS batch_category,
|
||||
rsu.name AS received_by_name
|
||||
FROM purchase_requests pr
|
||||
LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
|
||||
LEFT JOIN sso_users su ON pr.requester_id = su.user_id
|
||||
LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
|
||||
LEFT JOIN sso_users rsu ON pr.received_by = rsu.user_id
|
||||
WHERE pr.request_id = ?
|
||||
`, [requestId]);
|
||||
return rows[0] || null;
|
||||
@@ -105,6 +112,127 @@ const PurchaseRequestModel = {
|
||||
[requestId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 내 신청 목록 (모바일용, 페이지네이션)
|
||||
async getMyRequests(userId, { page = 1, limit = 20, status } = {}) {
|
||||
const db = await getDb();
|
||||
const offset = (page - 1) * limit;
|
||||
let where = 'WHERE pr.requester_id = ?';
|
||||
const params = [userId];
|
||||
|
||||
if (status) { where += ' AND pr.status = ?'; params.push(status); }
|
||||
|
||||
const [[{ total }]] = await db.query(
|
||||
`SELECT COUNT(*) AS total FROM purchase_requests pr ${where}`, params
|
||||
);
|
||||
|
||||
const [rows] = await db.query(`
|
||||
SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
|
||||
ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
|
||||
pr.custom_item_name, pr.custom_category,
|
||||
pb.batch_name, pb.status AS batch_status,
|
||||
rsu.name AS received_by_name
|
||||
FROM purchase_requests pr
|
||||
LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
|
||||
LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
|
||||
LEFT JOIN sso_users rsu ON pr.received_by = rsu.user_id
|
||||
${where}
|
||||
ORDER BY pr.created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`, [...params, limit, offset]);
|
||||
|
||||
return {
|
||||
data: rows,
|
||||
pagination: { page, limit, total, totalPages: Math.ceil(total / limit) }
|
||||
};
|
||||
},
|
||||
|
||||
// batch에 요청 그룹화 (status → grouped)
|
||||
async groupIntoBatch(requestIds, batchId) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests SET batch_id = ?, status = 'grouped'
|
||||
WHERE request_id IN (?) AND status = 'pending' AND batch_id IS NULL`,
|
||||
[batchId, requestIds]
|
||||
);
|
||||
},
|
||||
|
||||
// batch에서 제거 (status → pending 복원)
|
||||
async removeFromBatch(requestIds) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests SET batch_id = NULL, status = 'pending'
|
||||
WHERE request_id IN (?) AND status = 'grouped'`,
|
||||
[requestIds]
|
||||
);
|
||||
},
|
||||
|
||||
// batch 내 전체 요청 purchased 전환
|
||||
async markBatchPurchased(batchId) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests SET status = 'purchased' WHERE batch_id = ? AND status = 'grouped'`,
|
||||
[batchId]
|
||||
);
|
||||
},
|
||||
|
||||
// 개별 입고 처리
|
||||
async receive(requestId, { receivedPhotoPath, receivedLocation, receivedBy }) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests
|
||||
SET status = 'received', received_photo_path = ?, received_location = ?,
|
||||
received_at = NOW(), received_by = ?
|
||||
WHERE request_id = ? AND status = 'purchased'`,
|
||||
[receivedPhotoPath || null, receivedLocation || null, receivedBy, requestId]
|
||||
);
|
||||
return this.getById(requestId);
|
||||
},
|
||||
|
||||
// batch 내 전체 입고 처리
|
||||
async receiveBatch(batchId, { receivedPhotoPath, receivedLocation, receivedBy }) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests
|
||||
SET status = 'received', received_photo_path = ?, received_location = ?,
|
||||
received_at = NOW(), received_by = ?
|
||||
WHERE batch_id = ? AND status = 'purchased'`,
|
||||
[receivedPhotoPath || null, receivedLocation || null, receivedBy, batchId]
|
||||
);
|
||||
},
|
||||
|
||||
// batch 내 모든 요청이 received인지 확인
|
||||
async checkBatchAllReceived(batchId) {
|
||||
const db = await getDb();
|
||||
const [[{ total, received }]] = await db.query(
|
||||
`SELECT COUNT(*) AS total,
|
||||
SUM(CASE WHEN status = 'received' THEN 1 ELSE 0 END) AS received
|
||||
FROM purchase_requests WHERE batch_id = ?`,
|
||||
[batchId]
|
||||
);
|
||||
return total > 0 && total === received;
|
||||
},
|
||||
|
||||
// grouped 상태에서 hold (batch에서 자동 제거)
|
||||
async holdFromGrouped(requestId, holdReason) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_requests SET status = 'hold', hold_reason = ?, batch_id = NULL
|
||||
WHERE request_id = ? AND status = 'grouped'`,
|
||||
[holdReason || null, requestId]
|
||||
);
|
||||
return this.getById(requestId);
|
||||
},
|
||||
|
||||
// batch 내 신청자 ID 목록 조회
|
||||
async getRequesterIdsByBatch(batchId) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT DISTINCT requester_id FROM purchase_requests WHERE batch_id = ?`,
|
||||
[batchId]
|
||||
);
|
||||
return rows.map(r => r.requester_id);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user