Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW][API] Endpoints custom-user-status.create, custom-user-status.delete and custom-user-status.update to manage User Custom Status #16550

Merged
merged 3 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/api/server/v1/custom-user-status.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

import { CustomUserStatus } from '../../../models';
import { API } from '../api';
import { findCustomUserStatus } from '../lib/custom-user-status';

Expand All @@ -16,3 +20,69 @@ API.v1.addRoute('custom-user-status.list', { authRequired: true }, {
})));
},
});

API.v1.addRoute('custom-user-status.create', { authRequired: true }, {
post() {
check(this.bodyParams, {
name: String,
statusType: Match.Maybe(String),
});

const userStatusData = {
name: this.bodyParams.name,
statusType: this.bodyParams.statusType,
};

Meteor.runAsUser(this.userId, () => {
Meteor.call('insertOrUpdateUserStatus', userStatusData);
});

return API.v1.success({
customUserStatus: CustomUserStatus.findOneByName(userStatusData.name),
});
},
});

API.v1.addRoute('custom-user-status.delete', { authRequired: true }, {
post() {
const { customUserStatusId } = this.bodyParams;
if (!customUserStatusId) {
return API.v1.failure('The "customUserStatusId" params is required!');
}

Meteor.runAsUser(this.userId, () => Meteor.call('deleteCustomUserStatus', customUserStatusId));

return API.v1.success();
},
});

API.v1.addRoute('custom-user-status.update', { authRequired: true }, {
post() {
check(this.bodyParams, {
_id: String,
name: String,
statusType: Match.Maybe(String),
});

const userStatusData = {
_id: this.bodyParams._id,
name: this.bodyParams.name,
statusType: this.bodyParams.statusType,
};

const customUserStatus = CustomUserStatus.findOneById(userStatusData._id);

// Ensure the message exists
if (!customUserStatus) {
return API.v1.failure(`No custom user status found with the id of "${ userStatusData._id }".`);
}

Meteor.runAsUser(this.userId, () => {
Meteor.call('insertOrUpdateUserStatus', userStatusData);
});

return API.v1.success({
customUserStatus: CustomUserStatus.findOneById(userStatusData._id),
});
},
});
5 changes: 5 additions & 0 deletions app/models/server/models/CustomUserStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class CustomUserStatus extends Base {
return this.findOne(_id, options);
}

// find one by name
findOneByName(name, options) {
return this.findOne({ name }, options);
}

// find
findByName(name, options) {
const query = {
Expand Down