Skip to content

Commit

Permalink
fix user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Finkes committed Mar 29, 2019
1 parent 03d90d0 commit 7c94197
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
40 changes: 20 additions & 20 deletions server/src/api/user.ts
Expand Up @@ -230,28 +230,28 @@ export class UserApi {
}

@validate(deviceSchema)
registerDevice(req: Request, res: Response) {
let device = <IDevice>req.body;
Device.findOneAndUpdate({ deviceId:device.deviceId }, {
deviceId: device.deviceId,
pushId: device.pushId,
system: device.system,
async registerDevice(req: Request, res: Response) {
let payload = req.body as IDevice
try{
const device = await Device.findOneAndUpdate({ deviceId:payload.deviceId }, {
deviceId: payload.deviceId,
pushId: payload.pushId,
system: payload.system,
user: req.authenticatedUser._id
}, { upsert: true, new: true }).then(device => {
}, { upsert: true, new: true })

User.findById(req.authenticatedUser._id).then(user => {
if (user.devices.indexOf(device._id) != -1) {
sendSuccess(res, 201, {device:device})
} else {
user.devices.push(device._id);
user.save().then(() => {
sendSuccess(res, 201, {device:device})
})
}
})
}).catch(err => {
sendError(res, 500, err)
})
const user = await User.findById(req.authenticatedUser._id)
if (user.devices.indexOf(device._id) != -1) {
sendSuccess(res, 201, {device:device})
} else {
user.devices.push(device.id);
await user.save()
sendSuccess(res, 201, {device:device})
}
}
catch(error){
sendError(res, 500, error)
}
}

getMemberships(req: Request, res: Response) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/models/user.ts
Expand Up @@ -24,7 +24,7 @@ let userSchema = new mongoose.Schema({
registered: { type: Date, default: Date.now },
image: String,
devices : [{ type: mongoose.Schema.Types.ObjectId, ref: 'Device' }]
})
}, { usePushEach: true })

export let User = mongoose.model<IUserModel>('User', userSchema)

Expand Down

0 comments on commit 7c94197

Please sign in to comment.