skailan-conversations
Version:
Servicio de conversaciones y mensajería para Skailan
95 lines • 3.14 kB
JavaScript
import { Channel } from "../../../domain/entities/Channel";
export class ChannelPrismaRepository {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async findById(id) {
const channelData = await this.prisma.channel.findUnique({
where: { id },
});
if (!channelData) {
return null;
}
return Channel.fromData(channelData);
}
async findByType(type, organizationId) {
const channelData = await this.prisma.channel.findFirst({
where: { type, organizationId },
});
if (!channelData) {
return null;
}
return Channel.fromData(channelData);
}
async findByTypeAndOrganization(type, organizationId) {
const channelData = await this.prisma.channel.findFirst({
where: { type, organizationId },
});
if (!channelData) {
return null;
}
return Channel.fromData(channelData);
}
async findAll(organizationId) {
const channelsData = await this.prisma.channel.findMany({
where: { organizationId },
});
return channelsData.map((channelData) => Channel.fromData(channelData));
}
async create(channel) {
const savedChannel = await this.prisma.channel.create({
data: {
id: channel.id,
organizationId: channel.organizationId,
type: channel.type,
config: channel.config,
status: channel.status,
createdAt: channel.createdAt,
updatedAt: channel.updatedAt,
},
});
return Channel.fromData(savedChannel);
}
async update(channel) {
const updatedChannel = await this.prisma.channel.update({
where: { id: channel.id },
data: {
organizationId: channel.organizationId,
type: channel.type,
config: channel.config,
status: channel.status,
updatedAt: channel.updatedAt,
},
});
return Channel.fromData(updatedChannel);
}
async save(channel) {
const savedChannel = await this.prisma.channel.upsert({
where: { id: channel.id },
update: {
organizationId: channel.organizationId,
type: channel.type,
config: channel.config,
status: channel.status,
updatedAt: channel.updatedAt,
},
create: {
id: channel.id,
organizationId: channel.organizationId,
type: channel.type,
config: channel.config,
status: channel.status,
createdAt: channel.createdAt,
updatedAt: channel.updatedAt,
},
});
return Channel.fromData(savedChannel);
}
async delete(id, organizationId) {
await this.prisma.channel.delete({
where: { id },
});
}
}
//# sourceMappingURL=ChannelPrismaRepository.js.map