2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-08-17 02:30:46 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
const WelcomeMessages = {
|
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const message = await prisma.welcome_messages.findFirst({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return message || null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
2023-08-17 02:30:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit) {
|
|
|
|
try {
|
|
|
|
const messages = await prisma.welcome_messages.findMany({
|
|
|
|
where: clause,
|
|
|
|
take: limit || undefined,
|
|
|
|
});
|
|
|
|
return messages;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-08-17 02:30:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
saveAll: async function (messages) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
await prisma.welcome_messages.deleteMany({}); // Delete all existing messages
|
|
|
|
|
|
|
|
// Create new messages
|
2024-01-17 03:23:51 +01:00
|
|
|
// We create each message individually because prisma
|
|
|
|
// with sqlite does not support createMany()
|
2023-09-28 23:00:03 +02:00
|
|
|
for (const [index, message] of messages.entries()) {
|
2024-01-17 03:23:51 +01:00
|
|
|
if (!message.response) continue;
|
2023-09-28 23:00:03 +02:00
|
|
|
await prisma.welcome_messages.create({
|
|
|
|
data: {
|
|
|
|
user: message.user,
|
|
|
|
response: message.response,
|
|
|
|
orderIndex: index,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to save all messages", error.message);
|
2023-08-17 02:30:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getMessages: async function () {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const messages = await prisma.welcome_messages.findMany({
|
|
|
|
orderBy: { orderIndex: "asc" },
|
|
|
|
select: { user: true, response: true },
|
|
|
|
});
|
|
|
|
return messages;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to get all messages", error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-08-17 02:30:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.WelcomeMessages = WelcomeMessages;
|