Created
May 29, 2019 22:47
-
-
Save iErik/4abcf5bc58106759b1d9fb37aa2f4b2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request from '@Employee:graphql' | |
import * as get from '@Employee:graphql/queries' | |
import * as set from '@Employee:graphql/mutations' | |
import * as mappers from './mappers' | |
import * as formatters from './formatters' | |
/** | |
* Getters | |
* ------- | |
*/ | |
export const getPersonalDocuments = async (employeeId, employee) => { | |
const queries = { | |
rg: { query: 'GetRg', mapper: 'mapRg' }, | |
ctps: { query: 'GetCtps', mapper: 'mapCtps' }, | |
cpf: { query: 'GetCpf', mapper: 'mapCpf' }, | |
electoralCard: { query: 'GetElectoralCard', mapper: 'mapElectoralCard' }, | |
driverLicense: { query: 'GetDriverLicense', mapper: 'mapDriverLicense' }, | |
residenceProof: { query: 'GetResidenceProof', mapper: 'mapResidenceProof' }, | |
otherDocuments: { query: 'GetOtherDocuments', mapper: 'mapOtherDocuments' } | |
} | |
await Promise.all(Object.keys(queries).map(async docName => { | |
const { query, mapper: mapFn } = queries[docName] | |
const [err, data] = await request(get[query], { employeeId }) | |
const mappedData = data && mapFn | |
? mappers[mapFn]({ ...data, employee }) | |
: data | |
queries[docName] = mappedData | |
return [err, data] | |
})) | |
// TODO: Treat errors here, maybe. | |
return [null, queries] | |
} | |
export const getDocumentsOptions = async () => { | |
const [err, data] = await request(get.GetDocumentsOptions) | |
return [err, ((data || {}).optionsList || {})] | |
} | |
/** | |
* Setters | |
* ------- | |
*/ | |
const submitPayload = async ({ action, domainName, variables }) => { | |
const mutationName = `${action}${domainName}` | |
const [err, data] = await request(set[mutationName], { ...variables }) | |
return [err, data] | |
} | |
const getActionType = (action, { id }) => | |
action.toLowerCase().includes('delete') ? action : id ? 'Update' : 'Create' | |
/** | |
* Documents Mutation. | |
* ------------------ | |
* | |
* (yes, a single mutation that does a shit load of stuff) | |
* | |
* @param {String} docType - | |
* @param {String} action - | |
* @param {Object} variables - | |
* | |
* @returns {Promise} - | |
*/ | |
export const setDocument = ({ docType, action, variables }) => submitPayload({ | |
domainName: docType, | |
action: getActionType(action, variables), | |
variables: formatters[`format${docType}`](getActionType(action, variables), variables) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment