feat: friendships

This commit is contained in:
cha0s 2020-07-15 20:54:09 -05:00
parent af1336e859
commit e82c173939
2 changed files with 18 additions and 1 deletions

View File

@ -1,7 +1,14 @@
import {DataTypes as Types} from 'sequelize';
import BaseModel from './base'; import BaseModel from './base';
class Friendship extends BaseModel { class Friendship extends BaseModel {
static get attributes() {
return {
state: Types.ENUM(['pending', 'active']),
};
}
static associate({User}) { static associate({User}) {
User.hasMany(this, { User.hasMany(this, {
as: 'adder', as: 'adder',

View File

@ -44,7 +44,17 @@ class User extends BaseModel {
], ],
}, },
}); });
return friendships.map(({adderId, addeeId}) => (adderId === this.id ? addeeId : adderId)); return Promise.all(
friendships.map(async ({adderId, addeeId, state}) => {
const otherId = adderId === this.id ? addeeId : adderId;
const user = await User.findByPk(otherId);
return {
id: user.id,
redditUsername: user.redditUsername,
state,
};
}),
);
} }
validatePassword(plaintext) { validatePassword(plaintext) {