From 633e552b31823a92366c55792f4892a0851a270a Mon Sep 17 00:00:00 2001 From: vjrj Date: Wed, 13 Dec 2017 22:21:20 +0100 Subject: [PATCH] User schema wip --- imports/api/Users/Users.js | 106 +++++++++++++++++++++++ imports/api/Users/server/publications.js | 5 +- imports/api/Utility/Utils.js | 26 ++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 imports/api/Users/Users.js create mode 100644 imports/api/Utility/Utils.js diff --git a/imports/api/Users/Users.js b/imports/api/Users/Users.js new file mode 100644 index 0000000..d081b98 --- /dev/null +++ b/imports/api/Users/Users.js @@ -0,0 +1,106 @@ +/* eslint-disable consistent-return */ +/* eslint-disable import/no-absolute-path */ +import { Meteor } from 'meteor/meteor'; +import SimpleSchema from 'simpl-schema'; +import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js'; +import i18n from 'i18next'; + +const schemaUserProfile = new SimpleSchema({ + name: { type: String, optional: true }, + lang: { type: String, optional: true }, + telegramChatId: { type: Number, optional: true }, + telegramUsername: { type: String, optional: true }, + telegramFirstName: { type: String, optional: true }, + telegramLanguageCode: { type: String, optional: true }, + createdAt: defaultCreatedAt, + updatedAt: defaultUpdateAt +}); + +const schemaUser = new SimpleSchema({ + username: { + type: String, + // i18nLabel: 'Usuario/a', + regEx: /^[a-z0-9A-Z_]{3,15}$/, + // For accounts-password, either emails or username is required, + // but not both. + // It is OK to make this optional here because the accounts-password + // package + // does its own validation. + // Third-party login packages may not require either. + // Adjust this schema as necessary for your usage. + optional: true + }, + emails: { + type: Array, + min: 1, + label() { return i18n.t('Lista de emails de contacto'); }, + // For accounts-password, either emails or username is required, + // but not both. + // It is OK to make this optional here because the accounts-password + // package + // does its own validation. + // Third-party login packages may not require either. + // Adjust this schema as necessary for your usage. + optional: true + }, + 'emails.$': { + label: false, + type: Object + }, + 'emails.$.address': { + type: String, + label: '', + regEx: SimpleSchema.RegEx.Email + }, + 'emails.$.verified': { + type: Boolean, + optional: true // if not present === notVerified + }, + profile: { + type: schemaUserProfile, + // i18nLabel: 'Otros datos', + optional: true + }, + // Make sure this services field is in your schema + // if you're using any of the accounts packages + services: { + type: Object, + optional: true, + // autoform: { type: 'hidden' }, + blackbox: true + }, + // Add `roles` to your schema if you use the meteor-roles package. + // Option 1: Object type + // If you specify that type as Object, you must also specify the + // `Roles.GLOBAL_GROUP` group whenever you add a user to a role. + // Example: + // Roles.addUsersToRoles(userId, ['admin'], Roles.GLOBAL_GROUP); + // You can't mix and match adding with and without a group since + // you will fail validation in some cases. + // roles: { + // type: Object, + // optional: true, + // autoform: { type: 'hidden' }, + // blackbox: true + // } + // Option 2: [String] type + // If you are sure you will never need to use role groups, then + // you can specify [String] as the type + roles: { + type: Array, + // autoform: { type: 'hidden' }, + optional: true + }, + // https://github.com/todda00/meteor-friendly-slugs/issues/1 + friendlySlugs: { + type: Object, + optional: true, + blackbox: true + }, + slug: { + type: String, + optional: true + } +}); + +Meteor.users.attachSchema(schemaUser); diff --git a/imports/api/Users/server/publications.js b/imports/api/Users/server/publications.js index 7728b4b..f40a560 100644 --- a/imports/api/Users/server/publications.js +++ b/imports/api/Users/server/publications.js @@ -1,11 +1,12 @@ import { Meteor } from 'meteor/meteor'; +// import '../Users'; Meteor.publish('users.editProfile', function usersProfile() { return Meteor.users.find(this.userId, { fields: { emails: 1, profile: 1, - services: 1, - }, + services: 1 + } }); }); diff --git a/imports/api/Utility/Utils.js b/imports/api/Utility/Utils.js new file mode 100644 index 0000000..f69c11e --- /dev/null +++ b/imports/api/Utility/Utils.js @@ -0,0 +1,26 @@ +/* eslint-disable consistent-return */ + +export const defaultCreatedAt = { + type: Date, + // autoform: { type: 'hidden' }, + autoValue: () => { + if (this.isInsert) { + return new Date(); + } + // Prevent user from supplying their own value + this.unset(); + } +}; + +export const defaultUpdateAt = { + type: Date, + // autoform: { type: 'hidden' }, + autoValue: () => { + if (this.isUpdate || this.isInsert) { + return new Date(); + } + }, + // Commented because we want to have updateAt = createAt initialy + // denyInsert: true, + optional: true +};