You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
779 B
JavaScript

import mongoose, { model } from "mongoose";
const { Schema } = mongoose;
import { findUserBySpotifyId } from "../lib/helpers.mjs";
const userSchema = new Schema({
accessToken: String,
role: String,
spotify: {
refreshToken: String,
userId: String,
local: {
type: Boolean,
async get(local) {
if (!local) return false;
return await findUserBySpotifyId({
userId: this.spotify.userId,
refreshToken: this.spotify.refreshToken,
}, { create: true });
},
},
},
}, {
query: {
bySpotifyId(id) {
return this.where({ 'spotify.userId': id });
},
byAccessToken(accessToken) {
return this.where({ accessToken });
},
},
});
export const UserStore = model('User', userSchema);