Skip to content

Commit

Permalink
Merge pull request #21 from setlife-network/oscar
Browse files Browse the repository at this point in the history
API routes
  • Loading branch information
otech47 authored Jan 2, 2019
2 parents f186020 + 951bb65 commit f4fd98a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
37 changes: 37 additions & 0 deletions api/handlers/airtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ const base = new Airtable({

const airtable = module.exports = (function () {

const createRecord = (params) => {
return new Promise((resolve, reject) => {

base(params.tableName)
.create({
...params.fieldData
}, function(err, record) {
if (err) {
console.error(err);
reject(err);
} else {
resolve(record)
}
});
});
};

const deleteRecord = (params) => {
return new Promise((resolve, reject) => {

base(params.tableName)
.destroy(
params.recordId,
function(err, record) {
if (err) {
console.error(err);
reject(err);
} else {
resolve(record)
}
}
);
});
};

const fetchBaseRecords = (params) => {
return new Promise((resolve, reject) => {
let baseRecords = []
Expand Down Expand Up @@ -81,6 +116,8 @@ const airtable = module.exports = (function () {
};

return {
createRecord,
deleteRecord,
fetchBaseRecords,
fetchFilteredRecords,
fetchTableRecord
Expand Down
2 changes: 1 addition & 1 deletion api/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var apiModules = module.exports = (function() {
return {
schedules: require('./schedule'),
schedule: require('./schedule'),
team: require('./team')
};
})();
31 changes: 29 additions & 2 deletions api/modules/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,44 @@
var _ = require('lodash');

var schedule = module.exports = (function () {
const { fetchTeamMemberById } = require('./team')

const airtable = require('../handlers/airtable')

const createSetblock = function (params) {
return new Promise(function (resolve, reject) {
resolve('In progress')
airtable.createRecord({
tableName: 'Scheduling',
fieldData: {
Date: params.date,
Member: [
params.teamMemberId
],
Blocktime: params.blockTime,
Blocks: params.blockFraction,
Issue: params.issueUrl || '',
Description: params.description || ''
}
})
.then(newSetBlock => {
return fetchTeamMemberById({ id: params.teamMemberId })
})
.then(resolve)
.catch(reject)
});
};


const deleteSetblock = function (params) {
return new Promise(function (resolve, reject) {
resolve('In progress')
airtable.deleteRecord({
tableName: 'Scheduling',
recordId: params.setblockId
})
.then(() => {
resolve('Deleted Setblock: ' + params.setblockId)
})
.catch(reject)
});
};

Expand Down
12 changes: 9 additions & 3 deletions api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ var apiSchema = new g.GraphQLSchema(
type: new g.GraphQLNonNull(g.GraphQLString)
},
blockFraction: {
type: new g.GraphQLNonNull(g.GraphQLString)
type: new g.GraphQLNonNull(g.GraphQLFloat)
},
issueUrl: {
type: g.GraphQLString
},
description: {
type: g.GraphQLString
}
},

description: 'Adds a Setblock to TeamMember\'s schedule',
description: 'Adds a Setblock to TeamMember\'s schedule. Returns the specified TeamMember to allow for direct query to weeklySetblocks. May consider returning the SetblockType with the new ID instead if re-querying Airtable becomes a rate limit concern.',

type: g.GraphQLString,
type: types.TeamMemberType,

resolve: function (root, args) {
return apiModules.schedule.createSetblock(args);
Expand Down
23 changes: 13 additions & 10 deletions api/types/TeamMemberType.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var TeamMemberType = module.exports = new g.GraphQLObjectType({
weeklySetblocks: {
type: new g.GraphQLList(SetblockType),
description: 'Returns the current week\'s SetBlocks for this TeamMember, starting from the 1st Setblock on Monday to the last Setblock on Sunday',
resolve: () => {
resolve: (rootModel) => {
return new Promise((resolve, reject) => {

// Offset 1 day before start and 1 day after end
Expand All @@ -35,16 +35,19 @@ var TeamMemberType = module.exports = new g.GraphQLObjectType({
viewName: 'All'
})
.then(records => {
const setblocks = records.map(r => {
return {
id: r.id,
date: r.fields.Date,
blockTime: r.fields.Blocktime,
blockFraction: r.fields.Blocks,
issueUrl: r.fields.Issue,
description: r.fields.Description
const setblocks = records.reduce((array, r, i) => {
if (r.fields.Member[0] == rootModel.id) {
array.push({
id: r.id,
date: r.fields.Date,
blockTime: r.fields.Blocktime,
blockFraction: r.fields.Blocks,
issueUrl: r.fields.Issue,
description: r.fields.Description
})
}
})
return array
}, [])
resolve(setblocks)
})
})
Expand Down

0 comments on commit f4fd98a

Please sign in to comment.