From 79f88dcf75817d0a9a72f4f8cc70d612ca4149d7 Mon Sep 17 00:00:00 2001 From: Barry Low Date: Wed, 27 Mar 2019 18:03:51 +0100 Subject: [PATCH] Extend fbByOwner Query to return all FBs --- .../graphql/resolvers/Query/FinancialBeing.js | 8 ++- api/src/utils/index.js | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 api/src/utils/index.js diff --git a/api/src/graphql/resolvers/Query/FinancialBeing.js b/api/src/graphql/resolvers/Query/FinancialBeing.js index cca862c97..b1fa89dce 100644 --- a/api/src/graphql/resolvers/Query/FinancialBeing.js +++ b/api/src/graphql/resolvers/Query/FinancialBeing.js @@ -3,6 +3,8 @@ import { logger } from '../../../logger' import FB_FRAGMENT from '../../fragments/FBFragment' import FB_TEAM_FRAGMENT from '../../fragments/FBTeamFragment' +import { isDefined, isNull } from '../../../utils' + export const fbByFbId = async (parent, { fbId }, ctx, info) => { logger.info(`fbByFbId: ${fbId}`) let FB = await ctx.db.query.financialBeings({where: {id: fbId}}, FB_FRAGMENT) @@ -24,5 +26,9 @@ export const fbByOwner = async (parent, { fbId, fbSlug }, ctx, info) => { throw new AuthenticationError() return } - return ctx.db.query.financialBeingsesConnection({where: {AND:[{team:{members_some:{member:{authId: authId}}}},{OR:[{slug:fbSlug},{id:fbId}]}]}}, FB_TEAM_FRAGMENT) + if ((isDefined(fbId) && !isNull(fbId)) || (isDefined(fbSlug) && !isNull(fbSlug))){ + return ctx.db.query.financialBeingsesConnection({where: {AND:[{team:{members_some:{member:{authId: authId}}}},{OR:[{slug:fbSlug},{id:fbId}]}]}}, FB_TEAM_FRAGMENT) + } else { + return ctx.db.query.financialBeingsesConnection({where: {team:{members_some:{member:{authId: authId}}}}}, FB_TEAM_FRAGMENT) + } } diff --git a/api/src/utils/index.js b/api/src/utils/index.js new file mode 100644 index 000000000..e4649f06b --- /dev/null +++ b/api/src/utils/index.js @@ -0,0 +1,69 @@ +export const toLocalTime = (epoch) => { + const localDate = new Date(0); + localDate.setUTCSeconds(epoch); + const options = { year: 'numeric', month: 'long', day: '2-digit' }; + return ( + `${localDate.toLocaleDateString(options) + } at ${ + localDate.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' })}` + ); +}; + +export const temporaryFunction = input => input; + +export function isDefined (d) { + return d !== null && typeof d !== 'undefined' +} + +export function isNull (d) { + return d === null +} + +export const isArray = Array.isArray + +export function isObject (d) { + return isDefined(d) && typeof d === 'object' && !Array.isArray(d) +} + +export function isEmpty (obj) { + for (var key in obj) { + if (obj.hasOwnProperty(key)) return false + } + return true +} + +export function validObject (object, ...keys) { + return keys.reduce((a, b) => (a || { })[b], object) !== undefined +} + +export function mergeDeep (target, ...sources) { + if (!sources.length) return target + const source = sources.shift() + + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }) + mergeDeep(target[key], source[key]) + } else { + Object.assign(target, { [key]: source[key] }) + } + } + } + return mergeDeep(target, ...sources) +} + +export function slugify (string) { + const a = 'àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;' + const b = 'aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------' + const p = new RegExp(a.split('').join('|'), 'g') + + return string.toString().toLowerCase() + .replace(/\s+/g, '-') // Replace spaces with - + .replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters + .replace(/&/g, '-and-') // Replace & with 'and' + .replace(/[^\w-]+/g, '') // Remove all non-word characters + .replace(/--+/g, '-') // Replace multiple - with single - + .replace(/^-+/, '') // Trim - from start of text + .replace(/-+$/, '') // Trim - from end of text +}