2023-03-04 17:39:06 -05:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
|
|
|
|
|
const customGptSchema = mongoose.Schema({
|
|
|
|
|
chatGptLabel: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
promptPrefix: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
value: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
2023-03-16 13:30:20 +08:00
|
|
|
user: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2023-03-13 05:20:35 +08:00
|
|
|
}, { timestamps: true });
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
|
const CustomGpt = mongoose.models.CustomGpt || mongoose.model('CustomGpt', customGptSchema);
|
|
|
|
|
|
2023-03-16 13:30:20 +08:00
|
|
|
const createCustomGpt = async ({ chatGptLabel, promptPrefix, value, user }) => {
|
2023-03-04 17:39:06 -05:00
|
|
|
try {
|
|
|
|
|
await CustomGpt.create({
|
|
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
2023-03-16 13:30:20 +08:00
|
|
|
value,
|
|
|
|
|
user
|
2023-03-04 17:39:06 -05:00
|
|
|
});
|
|
|
|
|
return { chatGptLabel, promptPrefix, value };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return { customGpt: 'Error saving customGpt' };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2023-03-16 13:30:20 +08:00
|
|
|
getCustomGpts: async (user, filter) => {
|
2023-03-04 17:39:06 -05:00
|
|
|
try {
|
2023-03-16 13:32:04 -04:00
|
|
|
return await CustomGpt.find({ ...filter, user }).exec();
|
2023-03-04 17:39:06 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return { customGpt: 'Error getting customGpts' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-16 13:30:20 +08:00
|
|
|
updateCustomGpt: async (user, { value, ...update }) => {
|
2023-03-04 17:39:06 -05:00
|
|
|
try {
|
2023-03-16 13:32:04 -04:00
|
|
|
const customGpt = await CustomGpt.findOne({ value, user }).exec();
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
|
if (!customGpt) {
|
2023-03-16 13:32:04 -04:00
|
|
|
return await createCustomGpt({ value, ...update, user });
|
2023-03-04 17:39:06 -05:00
|
|
|
} else {
|
2023-03-16 13:32:04 -04:00
|
|
|
return await CustomGpt.findOneAndUpdate({ value, user }, update, {
|
2023-03-04 17:39:06 -05:00
|
|
|
new: true,
|
|
|
|
|
upsert: true
|
|
|
|
|
}).exec();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error updating customGpt' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-16 13:30:20 +08:00
|
|
|
updateByLabel: async (user, { prevLabel, ...update }) => {
|
2023-03-07 13:19:56 -05:00
|
|
|
try {
|
2023-03-16 13:32:04 -04:00
|
|
|
return await CustomGpt.findOneAndUpdate({ chatGptLabel: prevLabel, user }, update, {
|
2023-03-07 13:19:56 -05:00
|
|
|
new: true,
|
|
|
|
|
upsert: true
|
|
|
|
|
}).exec();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error updating customGpt' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-16 13:30:20 +08:00
|
|
|
deleteCustomGpts: async (user, filter) => {
|
2023-03-04 17:39:06 -05:00
|
|
|
try {
|
2023-03-16 13:32:04 -04:00
|
|
|
return await CustomGpt.deleteMany({ ...filter, user }).exec();
|
2023-03-04 17:39:06 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return { customGpt: 'Error deleting customGpts' };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|