Skip to content

Commit

Permalink
spead options and reduce statements
Browse files Browse the repository at this point in the history
  • Loading branch information
prabalsingh24 committed May 21, 2020
1 parent 04d4c4e commit ea4ff45
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
21 changes: 12 additions & 9 deletions api/graphql/query/Resource/availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ class ZoomAccountAvailability extends BaseResolver {
const topic = await resource.getTopic();

if (this.parent.subject_type === 'ZoomAccount') {
const key = {
id: this.parent.subject_id,
dateTimeRange: {
start_at: topic.start_at,
end_at: topic.end_at,
},
excludeTopics: [topic.id],
};
return (await ZoomAccount.findAllAvailabilityDuring([key]))[0];
return (
await ZoomAccount.findAllAvailabilityDuring([
{
id: this.parent.subject_id,
dateTimeRange: {
start_at: topic.start_at,
end_at: topic.end_at,
},
excludeTopics: [topic.id],
},
])
)[0];
}

//NOTE(naman) For a new resource add finding availability logic here
Expand Down
37 changes: 20 additions & 17 deletions api/services/ZoomAccount/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DataLoader from 'dataloader';
import Models from 'Models';
import DataLoader from 'dataloader';
import BaseModelService, {
saveInstance,
requireInstance,
Expand All @@ -10,16 +10,17 @@ export default class ZoomAccount extends BaseModelService {
static findAllInUse(...args) {
return Models.ZoomAccount.findAll(utilizedResourceClause(...args));
}
static async findAllAvailabilityDuring(keys) {
const resultPromises = keys.map(async (key) => {
const { id, ...args } = key;
return !(await Models.ZoomAccount.findOne({
where: { id },
...utilizedResourceClause(args),
}));
});

return Promise.all(resultPromises);
static async findAllAvailabilityDuring(keys) {
return Promise.all(
keys.map(
async ({ id, ...args }) =>
!(await Models.ZoomAccount.findOne({
where: { id },
...utilizedResourceClause(args),
})),
),
);
}

static getAvailabilityLoader() {
Expand All @@ -28,13 +29,15 @@ export default class ZoomAccount extends BaseModelService {

@requireInstance
async ifAvailableDuring(dateTimeRange, options) {
const key = {
id: this.instance.id,
dateTimeRange,
options,
};

return (await ZoomAccount.findAllAvailabilityDuring([key]))[0];
return (
await ZoomAccount.findAllAvailabilityDuring([
{
id: this.instance.id,
dateTimeRange,
...options,
},
])
)[0];
}

@saveInstance
Expand Down

0 comments on commit ea4ff45

Please sign in to comment.