skailan-contacts
Version:
Servicio de gestión de contactos para Skailan
144 lines • 5.67 kB
JavaScript
import { CreateTag } from '../../app/use-cases/CreateTag';
import { GetTagById } from '../../app/use-cases/GetTagById';
import { ListTags } from '../../app/use-cases/ListTags';
import { UpdateTag } from '../../app/use-cases/UpdateTag';
import { DeleteTag } from '../../app/use-cases/DeleteTag';
import { TagPrismaRepository } from '../../infra/database/prisma/TagPrismaRepository';
export const createTag = async (req, res) => {
try {
const { name } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!name || name.trim().length === 0) {
return res.status(400).json({ error: 'Tag name is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const tagRepository = new TagPrismaRepository(prisma);
const createTagUseCase = new CreateTag(tagRepository);
const newTag = await createTagUseCase.execute({
organizationId,
name,
});
res.status(201).json(newTag.toJSON());
}
catch (error) {
const statusCode = error.message.includes('already exists') ? 409 : 500;
res.status(statusCode).json({ error: error.message || 'Error creating tag.' });
}
};
export const getTagById = async (req, res) => {
try {
const { id } = req.params;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!id) {
return res.status(400).json({ error: 'Tag ID is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const tagRepository = new TagPrismaRepository(prisma);
const getTagByIdUseCase = new GetTagById(tagRepository);
const tag = await getTagByIdUseCase.execute({
id,
organizationId,
});
if (!tag) {
return res.status(404).json({ error: 'Tag not found.' });
}
res.status(200).json(tag.toJSON());
}
catch (error) {
res.status(500).json({ error: error.message || 'Error retrieving tag.' });
}
};
export const listTags = async (req, res) => {
try {
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const tagRepository = new TagPrismaRepository(prisma);
const listTagsUseCase = new ListTags(tagRepository);
const tags = await listTagsUseCase.execute({
organizationId,
});
res.status(200).json(tags.map(tag => tag.toJSON()));
}
catch (error) {
res.status(500).json({ error: error.message || 'Error listing tags.' });
}
};
export const updateTag = async (req, res) => {
try {
const { id } = req.params;
const { name } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!id) {
return res.status(400).json({ error: 'Tag ID is required.' });
}
if (!name || name.trim().length === 0) {
return res.status(400).json({ error: 'Tag name is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const tagRepository = new TagPrismaRepository(prisma);
const updateTagUseCase = new UpdateTag(tagRepository);
const updatedTag = await updateTagUseCase.execute({
id,
organizationId,
name,
});
res.status(200).json(updatedTag.toJSON());
}
catch (error) {
const statusCode = error.message.includes('not found') ? 404 :
error.message.includes('already exists') ? 409 : 500;
res.status(statusCode).json({ error: error.message || 'Error updating tag.' });
}
};
export const deleteTag = async (req, res) => {
try {
const { id } = req.params;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!id) {
return res.status(400).json({ error: 'Tag ID is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const tagRepository = new TagPrismaRepository(prisma);
const deleteTagUseCase = new DeleteTag(tagRepository);
await deleteTagUseCase.execute({
id,
organizationId,
});
res.status(204).send();
}
catch (error) {
const statusCode = error.message.includes('not found') ? 404 : 500;
res.status(statusCode).json({ error: error.message || 'Error deleting tag.' });
}
};
//# sourceMappingURL=tagController.js.map