init
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
const { SubscriptionPlan, SubscriptionPlanFeature, ClubSubscription, Feature, Club, User } = require('../models');
|
||||
|
||||
// 전체 구독 목록 조회
|
||||
exports.getClubSubscriptions = async (req, res) => {
|
||||
try {
|
||||
const subscriptions = await ClubSubscription.findAll({
|
||||
include: [
|
||||
{
|
||||
model: SubscriptionPlan,
|
||||
include: [{
|
||||
model: Feature,
|
||||
through: { attributes: ['status'] }
|
||||
}]
|
||||
},
|
||||
{
|
||||
model: Club,
|
||||
include: [{
|
||||
model: User,
|
||||
as: 'owner',
|
||||
attributes: ['id', 'name', 'email']
|
||||
}]
|
||||
}
|
||||
]
|
||||
});
|
||||
res.json(subscriptions);
|
||||
} catch (error) {
|
||||
console.error('Error in getClubSubscriptions:', error);
|
||||
res.status(500).json({ message: '구독 목록 조회 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 구독 플랜 목록 조회
|
||||
exports.getSubscriptionPlans = async (req, res) => {
|
||||
try {
|
||||
const plans = await SubscriptionPlan.findAll({
|
||||
include: [{
|
||||
model: Feature,
|
||||
through: { attributes: ['status'] }
|
||||
}]
|
||||
});
|
||||
res.json(plans);
|
||||
} catch (error) {
|
||||
console.error('Error in getSubscriptionPlans:', error);
|
||||
res.status(500).json({ message: '구독 플랜 목록 조회 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 구독 플랜 생성
|
||||
exports.createSubscriptionPlan = async (req, res) => {
|
||||
try {
|
||||
const { name, description, maxMembers, price, features } = req.body;
|
||||
|
||||
const plan = await SubscriptionPlan.create({
|
||||
name,
|
||||
description,
|
||||
maxMembers,
|
||||
price
|
||||
});
|
||||
|
||||
if (features && features.length > 0) {
|
||||
const featureAssociations = features.map(featureId => ({
|
||||
planId: plan.id,
|
||||
featureId,
|
||||
status: 'active'
|
||||
}));
|
||||
await SubscriptionPlanFeature.bulkCreate(featureAssociations);
|
||||
}
|
||||
|
||||
const createdPlan = await SubscriptionPlan.findByPk(plan.id, {
|
||||
include: [{
|
||||
model: Feature,
|
||||
through: { attributes: ['status'] }
|
||||
}]
|
||||
});
|
||||
|
||||
res.status(201).json(createdPlan);
|
||||
} catch (error) {
|
||||
console.error('Error in createSubscriptionPlan:', error);
|
||||
res.status(500).json({ message: '구독 플랜 생성 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 구독 플랜 수정
|
||||
exports.updateSubscriptionPlan = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, description, maxMembers, price, features } = req.body;
|
||||
|
||||
const plan = await SubscriptionPlan.findByPk(id);
|
||||
if (!plan) {
|
||||
return res.status(404).json({ message: '구독 플랜을 찾을 수 없습니다.' });
|
||||
}
|
||||
|
||||
await plan.update({
|
||||
name,
|
||||
description,
|
||||
maxMembers,
|
||||
price
|
||||
});
|
||||
|
||||
if (features) {
|
||||
// 기존 기능 연결 삭제
|
||||
await SubscriptionPlanFeature.destroy({
|
||||
where: { planId: id }
|
||||
});
|
||||
|
||||
// 새로운 기능 연결 생성
|
||||
if (features.length > 0) {
|
||||
const featureAssociations = features.map(featureId => ({
|
||||
planId: id,
|
||||
featureId,
|
||||
status: 'active'
|
||||
}));
|
||||
await SubscriptionPlanFeature.bulkCreate(featureAssociations);
|
||||
}
|
||||
}
|
||||
|
||||
const updatedPlan = await SubscriptionPlan.findByPk(id, {
|
||||
include: [{
|
||||
model: Feature,
|
||||
through: { attributes: ['status'] }
|
||||
}]
|
||||
});
|
||||
|
||||
res.json(updatedPlan);
|
||||
} catch (error) {
|
||||
console.error('Error in updateSubscriptionPlan:', error);
|
||||
res.status(500).json({ message: '구독 플랜 수정 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 구독 플랜 삭제
|
||||
exports.deleteSubscriptionPlan = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// 플랜이 존재하는지 확인
|
||||
const plan = await SubscriptionPlan.findByPk(id);
|
||||
if (!plan) {
|
||||
return res.status(404).json({ message: '구독 플랜을 찾을 수 없습니다.' });
|
||||
}
|
||||
|
||||
// 플랜과 연결된 기능 관계 삭제
|
||||
await SubscriptionPlanFeature.destroy({
|
||||
where: { planId: id }
|
||||
});
|
||||
|
||||
// 플랜 삭제
|
||||
await plan.destroy();
|
||||
|
||||
res.json({ message: '구독 플랜이 삭제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Error in deleteSubscriptionPlan:', error);
|
||||
res.status(500).json({ message: '구독 플랜 삭제 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 클럽 구독 생성/수정
|
||||
exports.subscribeClub = async (req, res) => {
|
||||
try {
|
||||
const { clubId, planId, startDate, expiryDate } = req.body;
|
||||
|
||||
// 기존 구독 확인
|
||||
let subscription = await ClubSubscription.findOne({
|
||||
where: { clubId, status: 'active' }
|
||||
});
|
||||
|
||||
if (subscription) {
|
||||
// 기존 구독 만료 처리
|
||||
await subscription.update({
|
||||
status: 'expired',
|
||||
updatedAt: new Date()
|
||||
});
|
||||
}
|
||||
|
||||
// 새로운 구독 생성
|
||||
subscription = await ClubSubscription.create({
|
||||
clubId,
|
||||
planId,
|
||||
startDate: new Date(startDate),
|
||||
expiryDate: new Date(expiryDate),
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
res.status(201).json(subscription);
|
||||
} catch (error) {
|
||||
console.error('Error in subscribeClub:', error);
|
||||
res.status(500).json({ message: '클럽 구독 처리 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 클럽의 현재 구독 정보 조회
|
||||
exports.getClubSubscription = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.params;
|
||||
|
||||
const subscription = await ClubSubscription.findOne({
|
||||
where: { clubId, status: 'active' },
|
||||
include: [{
|
||||
model: SubscriptionPlan,
|
||||
include: [{
|
||||
model: Feature,
|
||||
through: { attributes: ['status'] }
|
||||
}]
|
||||
}]
|
||||
});
|
||||
|
||||
if (!subscription) {
|
||||
return res.status(404).json({ message: '활성화된 구독 정보가 없습니다.' });
|
||||
}
|
||||
|
||||
res.json(subscription);
|
||||
} catch (error) {
|
||||
console.error('Error in getClubSubscription:', error);
|
||||
res.status(500).json({ message: '구독 정보 조회 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
|
||||
// 플랜별 구독 목록 조회
|
||||
exports.getPlanSubscriptions = async (req, res) => {
|
||||
try {
|
||||
const { planId } = req.params;
|
||||
|
||||
const subscriptions = await ClubSubscription.findAll({
|
||||
where: { planId },
|
||||
include: [{
|
||||
model: Club,
|
||||
attributes: ['id', 'name']
|
||||
}]
|
||||
});
|
||||
|
||||
res.json(subscriptions);
|
||||
} catch (error) {
|
||||
console.error('Error in getPlanSubscriptions:', error);
|
||||
res.status(500).json({ message: '플랜별 구독 목록 조회 중 오류가 발생했습니다.' });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user