File size: 4,138 Bytes
f0743f4 | 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import type { DeleteResult, Model } from 'mongoose';
import type {
FindPluginAuthsByKeysParams,
UpdatePluginAuthParams,
DeletePluginAuthParams,
FindPluginAuthParams,
IPluginAuth,
} from '~/types';
// Factory function that takes mongoose instance and returns the methods
export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
/**
* Finds a single plugin auth entry by userId and authField (and optionally pluginKey)
*/
async function findOnePluginAuth({
userId,
authField,
pluginKey,
}: FindPluginAuthParams): Promise<IPluginAuth | null> {
try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.findOne({
userId,
authField,
...(pluginKey && { pluginKey }),
}).lean();
} catch (error) {
throw new Error(
`Failed to find plugin auth: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
/**
* Finds multiple plugin auth entries by userId and pluginKeys
*/
async function findPluginAuthsByKeys({
userId,
pluginKeys,
}: FindPluginAuthsByKeysParams): Promise<IPluginAuth[]> {
try {
if (!pluginKeys || pluginKeys.length === 0) {
return [];
}
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.find({
userId,
pluginKey: { $in: pluginKeys },
}).lean();
} catch (error) {
throw new Error(
`Failed to find plugin auths: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
/**
* Updates or creates a plugin auth entry
*/
async function updatePluginAuth({
userId,
authField,
pluginKey,
value,
}: UpdatePluginAuthParams): Promise<IPluginAuth> {
try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
const existingAuth = await PluginAuth.findOne({ userId, pluginKey, authField }).lean();
if (existingAuth) {
return await PluginAuth.findOneAndUpdate(
{ userId, pluginKey, authField },
{ $set: { value } },
{ new: true, upsert: true },
).lean();
} else {
const newPluginAuth = await new PluginAuth({
userId,
authField,
value,
pluginKey,
});
await newPluginAuth.save();
return newPluginAuth.toObject();
}
} catch (error) {
throw new Error(
`Failed to update plugin auth: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
/**
* Deletes plugin auth entries based on provided parameters
*/
async function deletePluginAuth({
userId,
authField,
pluginKey,
all = false,
}: DeletePluginAuthParams): Promise<DeleteResult> {
try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
if (all) {
const filter: DeletePluginAuthParams = { userId };
if (pluginKey) {
filter.pluginKey = pluginKey;
}
return await PluginAuth.deleteMany(filter);
}
if (!authField) {
throw new Error('authField is required when all is false');
}
return await PluginAuth.deleteOne({ userId, authField });
} catch (error) {
throw new Error(
`Failed to delete plugin auth: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
/**
* Deletes all plugin auth entries for a user
*/
async function deleteAllUserPluginAuths(userId: string): Promise<DeleteResult> {
try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.deleteMany({ userId });
} catch (error) {
throw new Error(
`Failed to delete all user plugin auths: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
return {
findOnePluginAuth,
findPluginAuthsByKeys,
updatePluginAuth,
deletePluginAuth,
deleteAllUserPluginAuths,
};
}
export type PluginAuthMethods = ReturnType<typeof createPluginAuthMethods>;
|