Spaces:
Paused
Paused
File size: 3,571 Bytes
3d52d95 04f3552 3d52d95 04f3552 3d52d95 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | const prisma = require('../lib/prisma');
const createWorkspace = async (req, res) => {
const { domain, websiteName, description, niche, country, language, monthlyTraffic } = req.body;
if (!domain || !websiteName || !description) {
return res.status(400).json({ error: 'Domain, website name, and description are required' });
}
if (!domain.toLowerCase().trim().endsWith('.com')) {
return res.status(400).json({ error: 'Only .com domains are allowed.' });
}
try {
// Check user doesn't already have a workspace
const existing = await prisma.teamMember.findFirst({
where: { userId: req.user.userId, role: 'OWNER' },
});
if (existing) {
return res.status(409).json({ error: 'User already has a workspace' });
}
const workspace = await prisma.workspace.create({
data: {
domain: domain.toLowerCase().replace(/^https?:\/\//, '').replace(/\/$/, ''),
websiteName,
description,
niche: niche || null,
country: country || null,
language: language || null,
monthlyTraffic: monthlyTraffic ? parseInt(monthlyTraffic) : null,
teamMembers: {
create: { userId: req.user.userId, role: 'OWNER' },
},
},
include: { teamMembers: true },
});
res.status(201).json({ workspace });
} catch (err) {
if (err.code === 'P2002') {
return res.status(409).json({ error: 'Domain already registered' });
}
console.error(err);
res.status(500).json({ error: 'Server error' });
}
};
const getMyWorkspace = async (req, res) => {
try {
const member = await prisma.teamMember.findFirst({
where: { userId: req.user.userId },
include: { workspace: true },
});
if (!member) return res.status(404).json({ error: 'No workspace found' });
res.json({ workspace: member.workspace, role: member.role });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
};
const getAllWorkspaces = async (req, res) => {
try {
const workspaces = await prisma.workspace.findMany({
select: { id: true, domain: true, websiteName: true, description: true, niche: true, country: true },
});
res.json({ workspaces });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
};
const updateMyWorkspace = async (req, res) => {
const { domain, websiteName, description, niche } = req.body;
try {
const member = await prisma.teamMember.findFirst({
where: { userId: req.user.userId, role: 'OWNER' },
});
if (!member) {
return res.status(403).json({ error: 'Only owners can update workspace details' });
}
if (domain && !domain.toLowerCase().trim().endsWith('.com')) {
return res.status(400).json({ error: 'Only .com domains are allowed.' });
}
const updated = await prisma.workspace.update({
where: { id: member.workspaceId },
data: {
domain: domain ? domain.toLowerCase().replace(/^https?:\/\//, '').replace(/\/$/, '') : undefined,
websiteName: websiteName || undefined,
description: description || undefined,
niche: niche || undefined,
},
});
res.json({ workspace: updated });
} catch (err) {
if (err.code === 'P2002') {
return res.status(409).json({ error: 'Domain already registered' });
}
console.error(err);
res.status(500).json({ error: 'Server error' });
}
};
module.exports = {
createWorkspace,
getMyWorkspace,
getAllWorkspaces,
updateMyWorkspace,
};
|