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:
35
system1-factory/api/models/itemAliasModel.js
Normal file
35
system1-factory/api/models/itemAliasModel.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// models/itemAliasModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
const ItemAliasModel = {
|
||||
async getAll() {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT ia.*, ci.item_name, ci.spec, ci.maker, ci.category
|
||||
FROM item_aliases ia
|
||||
JOIN consumable_items ci ON ia.item_id = ci.item_id
|
||||
ORDER BY ci.item_name, ia.alias_name`
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async create(itemId, aliasName) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO item_aliases (item_id, alias_name) VALUES (?, ?)`,
|
||||
[itemId, aliasName.trim()]
|
||||
);
|
||||
return result.insertId;
|
||||
},
|
||||
|
||||
async delete(aliasId) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`DELETE FROM item_aliases WHERE alias_id = ?`,
|
||||
[aliasId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ItemAliasModel;
|
||||
117
system1-factory/api/models/purchaseBatchModel.js
Normal file
117
system1-factory/api/models/purchaseBatchModel.js
Normal file
@@ -0,0 +1,117 @@
|
||||
// models/purchaseBatchModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
const PurchaseBatchModel = {
|
||||
async getAll(filters = {}) {
|
||||
const db = await getDb();
|
||||
let sql = `
|
||||
SELECT pb.*, su.name AS created_by_name,
|
||||
v.vendor_name,
|
||||
(SELECT COUNT(*) FROM purchase_requests WHERE batch_id = pb.batch_id) AS request_count
|
||||
FROM purchase_batches pb
|
||||
LEFT JOIN sso_users su ON pb.created_by = su.user_id
|
||||
LEFT JOIN vendors v ON pb.vendor_id = v.vendor_id
|
||||
WHERE 1=1
|
||||
`;
|
||||
const params = [];
|
||||
if (filters.status) { sql += ' AND pb.status = ?'; params.push(filters.status); }
|
||||
sql += ' ORDER BY pb.created_at DESC';
|
||||
const [rows] = await db.query(sql, params);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async getById(batchId) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(`
|
||||
SELECT pb.*, su.name AS created_by_name, v.vendor_name
|
||||
FROM purchase_batches pb
|
||||
LEFT JOIN sso_users su ON pb.created_by = su.user_id
|
||||
LEFT JOIN vendors v ON pb.vendor_id = v.vendor_id
|
||||
WHERE pb.batch_id = ?
|
||||
`, [batchId]);
|
||||
return rows[0] || null;
|
||||
},
|
||||
|
||||
async create({ batchName, category, vendorId, notes, createdBy }) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO purchase_batches (batch_name, category, vendor_id, notes, created_by)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[batchName || null, category || null, vendorId || null, notes || null, createdBy]
|
||||
);
|
||||
return result.insertId;
|
||||
},
|
||||
|
||||
async update(batchId, { batchName, category, vendorId, notes }) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_batches SET batch_name = ?, category = ?, vendor_id = ?, notes = ?
|
||||
WHERE batch_id = ? AND status = 'pending'`,
|
||||
[batchName || null, category || null, vendorId || null, notes || null, batchId]
|
||||
);
|
||||
return this.getById(batchId);
|
||||
},
|
||||
|
||||
async delete(batchId) {
|
||||
const db = await getDb();
|
||||
// pending 상태만 삭제 가능
|
||||
const [batch] = await db.query('SELECT status FROM purchase_batches WHERE batch_id = ?', [batchId]);
|
||||
if (!batch.length || batch[0].status !== 'pending') return false;
|
||||
|
||||
// 포함된 요청 복원
|
||||
await db.query(
|
||||
`UPDATE purchase_requests SET batch_id = NULL, status = 'pending' WHERE batch_id = ?`,
|
||||
[batchId]
|
||||
);
|
||||
await db.query('DELETE FROM purchase_batches WHERE batch_id = ?', [batchId]);
|
||||
return true;
|
||||
},
|
||||
|
||||
async markPurchased(batchId, purchasedBy) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_batches SET status = 'purchased', purchased_at = NOW(), purchased_by = ?
|
||||
WHERE batch_id = ? AND status = 'pending'`,
|
||||
[purchasedBy, batchId]
|
||||
);
|
||||
},
|
||||
|
||||
async markReceived(batchId, receivedBy) {
|
||||
const db = await getDb();
|
||||
await db.query(
|
||||
`UPDATE purchase_batches SET status = 'received', received_at = NOW(), received_by = ?
|
||||
WHERE batch_id = ? AND status = 'purchased'`,
|
||||
[receivedBy, batchId]
|
||||
);
|
||||
},
|
||||
|
||||
// batch에 요청 추가 (검증: pending이고 다른 batch에 속하지 않음)
|
||||
async addRequests(batchId, requestIds) {
|
||||
const db = await getDb();
|
||||
const [existing] = await db.query(
|
||||
`SELECT request_id, batch_id, status FROM purchase_requests WHERE request_id IN (?)`,
|
||||
[requestIds]
|
||||
);
|
||||
const invalid = existing.filter(r => r.status !== 'pending' || r.batch_id !== null);
|
||||
if (invalid.length > 0) {
|
||||
const ids = invalid.map(r => r.request_id);
|
||||
throw new Error(`다음 요청은 추가할 수 없습니다 (이미 그룹 소속이거나 대기 상태가 아님): ${ids.join(', ')}`);
|
||||
}
|
||||
|
||||
const PurchaseRequestModel = require('./purchaseRequestModel');
|
||||
await PurchaseRequestModel.groupIntoBatch(requestIds, batchId);
|
||||
},
|
||||
|
||||
// batch에서 요청 제거
|
||||
async removeRequests(batchId, requestIds) {
|
||||
const db = await getDb();
|
||||
const [batch] = await db.query('SELECT status FROM purchase_batches WHERE batch_id = ?', [batchId]);
|
||||
if (!batch.length || batch[0].status !== 'pending') {
|
||||
throw new Error('진행중인 그룹에서만 요청을 제거할 수 있습니다.');
|
||||
}
|
||||
const PurchaseRequestModel = require('./purchaseRequestModel');
|
||||
await PurchaseRequestModel.removeFromBatch(requestIds);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = PurchaseBatchModel;
|
||||
@@ -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