Here’s the code
import { Meteor } from 'meteor/meteor'
// import { Accounts } from 'meteor/accounts-base' <== RUNTIME ERROR
import { BaseCollection } from './BaseCollection'
import { SimpleSchema } from 'meteor/aldeed:simple-schema'
import { UserType, taxIdTypes } from '../helpers/consts'
export const AccountsCollection = new BaseCollection('accounts')
AccountsCollection.schema = new SimpleSchema({
shortName: { type: String, optional: false },
legalName: { type: String, optional: false },
address: { type: String },
city: { type: String },
state: { type: String },
zip: { type: String, optional: true },
country: { type: String, defaultValue: 'US' },
techContact: { type: Object, optional: false },
'techContact.firstName': { type: String, optional: false },
'techContact.lastName': { type: String, optional: false },
'techContact.email': { type: String, optional: false },
'techContact.phone': { type: String, optional: true },
billingContact: { type: Object },
'billingContact.firstName': { type: String, optional: false },
'billingContact.lastName': { type: String, optional: false },
'billingContact.email': { type: String, optional: false },
'billingContact.phone': { type: String, optional: true },
active: { type: Boolean, optional: true, defaultValue: true }
})
AccountsCollection.attachSchema(AccountsCollection.schema)
// Creates the account and adds the admin user with a random password
const createAccount = async (account) => {
const accountId = await AccountsCollection.insert(account)
// Add admin user for this account
const adminUser = {
username: account.techContact.email,
email: account.techContact.email,
profile: {
type: 'accountAdmin',
account: accountId,
firstName: account.techContact.firstName,
lastName: account.techContact.lastName,
phone: account.techContact.phone,
address: account.address,
city: account.city,
state: account.state,
zip: account.zip,
country: account.country
}
}
if (Meteor.isServer) {
// eslint-disable-next-line no-undef <== ESLint doesn't recognize 'Accounts'
await Accounts.createUserVerifyingEmail(adminUser) // <== Error when import is present
}
return accountId
}
Meteor.methods({
'account.create' (account) {
if (!Meteor.userId()) {
throw new Meteor.Error('Not logged in')
}
const user = Meteor.user()
if (user.profile.type !== UserType.globalAdmin) {
throw new Meteor.Error('Not authorized')
}
try {
AccountsCollection.schema.validate(account)
} catch (error) {
throw new Meteor.Error(`Invalid request: ${error.message}`)
}
const checkByShortName = AccountsCollection.findOne({ shortName: account.shortName })
if (checkByShortName) {
throw new Meteor.Error('Account with this short name already exists')
}
const checkUser = Meteor.users.findOne({ username: account.techContact.email })
if (checkUser) {
throw new Meteor.Error('User with this email already exists')
}
return createAccount(account)
}
})
if (Meteor.isServer) {
AccountsCollection.createIndex({ shortName: 1 }, { unique: true })
Meteor.publish('accounts', function () {
if (!this.userId) {
return this.ready()
}
const user = Meteor.users.findOne(this.userId)
if (!user) {
return this.ready()
}
if (user.profile.type === UserType.globalAdmin) {
return AccountsCollection.find()
}
throw new Meteor.Error('Not authorized')
})
}