init
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
const { Feature, ClubFeature, Club } = require('../models');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
// 전체 기능 목록 조회
|
||||
exports.getFeatures = async (req, res) => {
|
||||
try {
|
||||
const features = await Feature.findAll({
|
||||
where: {
|
||||
status: 'active'
|
||||
},
|
||||
attributes: ['id', 'name', 'description', 'price']
|
||||
});
|
||||
res.json(features);
|
||||
} catch (error) {
|
||||
console.error('기능 목록 조회 오류:', error);
|
||||
res.status(500).json({ message: '기능 목록을 가져오는 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 사용 가능한 기능 목록 조회
|
||||
exports.getAvailableFeatures = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.query;
|
||||
|
||||
if (!clubId) {
|
||||
return res.status(400).json({ message: '클럽 ID가 필요합니다.' });
|
||||
}
|
||||
|
||||
// 모든 기능 목록 조회
|
||||
const features = await Feature.findAll({
|
||||
where: {
|
||||
status: 'active'
|
||||
},
|
||||
attributes: ['id', 'name', 'description', 'price']
|
||||
});
|
||||
|
||||
// 클럽의 활성화된 기능 조회
|
||||
const activeFeatures = await ClubFeature.findAll({
|
||||
where: {
|
||||
clubId,
|
||||
expiryDate: {
|
||||
[Op.gt]: new Date()
|
||||
}
|
||||
},
|
||||
attributes: ['featureId']
|
||||
});
|
||||
|
||||
const activeFeatureIds = activeFeatures.map(af => af.featureId);
|
||||
|
||||
// 기능 목록에 활성화 상태 추가
|
||||
const formattedFeatures = features.map(feature => ({
|
||||
...feature.toJSON(),
|
||||
isActive: activeFeatureIds.includes(feature.id)
|
||||
}));
|
||||
|
||||
res.json(formattedFeatures);
|
||||
} catch (error) {
|
||||
console.error('사용 가능한 기능 목록 조회 오류:', error);
|
||||
res.status(500).json({ message: '기능 목록을 가져오는 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 활성화된 기능 목록 조회
|
||||
exports.getActiveFeatures = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.query;
|
||||
|
||||
if (!clubId) {
|
||||
return res.status(400).json({ message: '클럽 ID가 필요합니다.' });
|
||||
}
|
||||
|
||||
const activeFeatures = await ClubFeature.findAll({
|
||||
where: {
|
||||
clubId,
|
||||
expiryDate: {
|
||||
[Op.gt]: new Date()
|
||||
}
|
||||
},
|
||||
include: [{
|
||||
model: Feature,
|
||||
as: 'feature',
|
||||
attributes: ['name', 'description']
|
||||
}],
|
||||
attributes: ['id', 'featureId', 'purchaseDate', 'expiryDate']
|
||||
});
|
||||
|
||||
res.json(activeFeatures);
|
||||
} catch (error) {
|
||||
console.error('활성화된 기능 목록 조회 오류:', error);
|
||||
res.status(500).json({ message: '활성화된 기능 목록을 가져오는 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 기능 구매 내역 조회
|
||||
exports.getFeaturePayments = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.query;
|
||||
|
||||
if (!clubId) {
|
||||
return res.status(400).json({ message: '클럽 ID가 필요합니다.' });
|
||||
}
|
||||
|
||||
const payments = await ClubFeature.findAll({
|
||||
where: {
|
||||
clubId
|
||||
},
|
||||
include: [{
|
||||
model: Feature,
|
||||
as: 'feature',
|
||||
attributes: ['name']
|
||||
}],
|
||||
attributes: [
|
||||
'id',
|
||||
'featureId',
|
||||
'purchaseDate',
|
||||
'expiryDate',
|
||||
'amount',
|
||||
'duration',
|
||||
'status'
|
||||
],
|
||||
order: [['purchaseDate', 'DESC']]
|
||||
});
|
||||
|
||||
const formattedPayments = payments.map(payment => ({
|
||||
id: payment.id,
|
||||
featureName: payment.feature.name,
|
||||
purchaseDate: payment.purchaseDate,
|
||||
expiryDate: payment.expiryDate,
|
||||
amount: payment.amount,
|
||||
duration: payment.duration,
|
||||
status: payment.status
|
||||
}));
|
||||
|
||||
res.json(formattedPayments);
|
||||
} catch (error) {
|
||||
console.error('기능 구매 내역 조회 오류:', error);
|
||||
res.status(500).json({ message: '구매 내역을 가져오는 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 기능 구매
|
||||
exports.purchaseFeature = async (req, res) => {
|
||||
try {
|
||||
const { clubId, featureId, duration } = req.body;
|
||||
|
||||
if (!clubId) {
|
||||
return res.status(400).json({ message: '클럽 ID가 필요합니다.' });
|
||||
}
|
||||
|
||||
// 기능 정보 조회
|
||||
const feature = await Feature.findByPk(featureId);
|
||||
if (!feature) {
|
||||
return res.status(404).json({ message: '존재하지 않는 기능입니다.' });
|
||||
}
|
||||
|
||||
// 클럽 정보 조회
|
||||
const club = await Club.findByPk(clubId);
|
||||
if (!club) {
|
||||
return res.status(404).json({ message: '존재하지 않는 클럽입니다.' });
|
||||
}
|
||||
|
||||
// 이미 활성화된 기능인지 확인
|
||||
const existingFeature = await ClubFeature.findOne({
|
||||
where: {
|
||||
clubId,
|
||||
featureId,
|
||||
expiryDate: {
|
||||
[Op.gt]: new Date()
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (existingFeature) {
|
||||
return res.status(400).json({ message: '이미 활성화된 기능입니다.' });
|
||||
}
|
||||
|
||||
// 구매 금액 계산 (duration에 따른 할인 적용 가능)
|
||||
const amount = feature.price * duration;
|
||||
|
||||
// 기능 구매 기록 생성
|
||||
const purchaseDate = new Date();
|
||||
const expiryDate = new Date();
|
||||
expiryDate.setMonth(expiryDate.getMonth() + duration);
|
||||
|
||||
const clubFeature = await ClubFeature.create({
|
||||
clubId,
|
||||
featureId,
|
||||
purchaseDate,
|
||||
expiryDate,
|
||||
amount,
|
||||
duration,
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: '기능 구매가 완료되었습니다.',
|
||||
purchaseInfo: clubFeature
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('기능 구매 오류:', error);
|
||||
res.status(500).json({ message: '기능 구매 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user