Skip to content

Commit

Permalink
fix: save org-email (#587)
Browse files Browse the repository at this point in the history
* fix(create-org): pass email to service when creating org

* fix: support email when editing org

* fix: add email when creating new org
  • Loading branch information
Birkbjo authored Nov 30, 2021
1 parent fd2de7a commit e76ed33
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
18 changes: 16 additions & 2 deletions client/src/pages/UserOrganisation/Modals/EditNameModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ReactFinalForm,
InputFieldFF,
hasValue,
composeValidators,
email,
} from '@dhis2/ui'
import PropTypes from 'prop-types'
import styles from './Modal.module.css'
Expand All @@ -17,12 +19,13 @@ const EditNameModal = ({ organisation, mutate, onClose }) => {
const successAlert = useSuccessAlert()
const errorAlert = useErrorAlert()

const handleSubmit = async ({ name }) => {
const handleSubmit = async ({ name, email }) => {
try {
await api.editOrganisation(organisation.id, { name })
await api.editOrganisation(organisation.id, { name, email })
mutate({
...organisation,
name,
email,
})
successAlert.show({
message: `Successfully updated organisation name to ${name}`,
Expand Down Expand Up @@ -50,6 +53,17 @@ const EditNameModal = ({ organisation, mutate, onClose }) => {
className={styles.field}
validate={hasValue}
/>
<ReactFinalForm.Field
required
name="email"
label="Contact email"
placeholder="Enter an email address"
type="email"
component={InputFieldFF}
initialValue={organisation.email}
className={styles.field}
validate={composeValidators(hasValue, email)}
/>
<ButtonStrip end>
<Button onClick={onClose} disabled={submitting}>
Cancel
Expand Down
26 changes: 12 additions & 14 deletions server/src/routes/v2/organisations.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const Boom = require('@hapi/boom')
const Bounce = require('@hapi/bounce')
const { wrapError, UniqueViolationError } = require('db-errors')
const JWT = require('jsonwebtoken')
const Joi = require('../../utils/CustomJoi')
const getUserByEmail = require('../../data/getUserByEmail')
const OrgModel = require('../../models/v2/Organisation')
const {
currentUserIsManager,
getCurrentUserFromRequest,
} = require('../../security')
const getUserByEmail = require('../../data/getUserByEmail')
const { Organisation } = require('../../services')
const OrgModel = require('../../models/v2/Organisation')
const Joi = require('../../utils/CustomJoi')
// const debug = require('debug')('apphub:server:routes:handlers:organisations')
const { wrapError, UniqueViolationError } = require('db-errors')
const getServerUrl = require('../../utils/getServerUrl')

module.exports = [
Expand Down Expand Up @@ -87,9 +87,7 @@ module.exports = [
const { orgIdOrSlug } = request.params

const isUuid =
Joi.string()
.uuid()
.validate(orgIdOrSlug).error === undefined
Joi.string().uuid().validate(orgIdOrSlug).error === undefined

let organisation
if (isUuid) {
Expand Down Expand Up @@ -132,7 +130,11 @@ module.exports = [

const createOrgAndAddUser = async trx => {
const organisation = await Organisation.create(
{ userId, name: request.payload.name },
{
userId,
name: request.payload.name,
email: request.payload.email,
},
trx
)
await Organisation.addUserById(organisation.id, userId, trx)
Expand Down Expand Up @@ -200,9 +202,7 @@ module.exports = [
tags: ['api', 'v2'],
validate: {
payload: Joi.object({
email: Joi.string()
.email()
.required(),
email: Joi.string().email().required(),
}),
params: Joi.object({
orgId: OrgModel.definition.extract('id').required(),
Expand Down Expand Up @@ -338,9 +338,7 @@ module.exports = [
tags: ['api', 'v2'],
validate: {
payload: Joi.object({
email: Joi.string()
.email()
.required(),
email: Joi.string().email().required(),
skipSend: Joi.boolean().default(false),
}),
params: Joi.object({
Expand Down
17 changes: 7 additions & 10 deletions server/src/services/organisation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { NotFoundError } = require('../utils/errors')
const { slugify } = require('../utils/slugify')
const Organisation = require('../models/v2/Organisation')
const Boom = require('@hapi/boom')
const JWT = require('jsonwebtoken')
const Organisation = require('../models/v2/Organisation')
const { NotFoundError } = require('../utils/errors')
const { slugify } = require('../utils/slugify')

const getOrganisationQuery = db =>
db('organisation').select(
Expand Down Expand Up @@ -45,12 +45,13 @@ const ensureUniqueSlug = async (slug, knex) => {
* @param {string} data.userId User id creating the data
* @param {*} db
*/
const create = async ({ userId, name }, db) => {
const create = async ({ userId, name, email }, db) => {
const slug = await ensureUniqueSlug(slugify(name), db)
const obj = {
owner: userId,
name,
slug,
email,
}
const dbData = Organisation.formatDatabaseJson(obj)
const [organisation] = await db('organisation')
Expand Down Expand Up @@ -138,9 +139,7 @@ const update = async (id, updateData, db) => {
dbData.slug = slug
}

return db('organisation')
.update(dbData)
.where({ id })
return db('organisation').update(dbData).where({ id })
}

const addUserById = async (id, userId, db) => {
Expand All @@ -162,9 +161,7 @@ const removeUser = async (id, userId, db) => {
return query
}
const remove = async (id, db) => {
await db('organisation')
.where({ id })
.delete()
await db('organisation').where({ id }).delete()
}

const hasUser = async (id, userId, knex) => {
Expand Down

0 comments on commit e76ed33

Please sign in to comment.