57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
|
|
"use strict";
|
||
|
|
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2021*/
|
||
|
|
/**
|
||
|
|
* refer_log Resolve All
|
||
|
|
* @copyright 2021 Manaknightdigital Inc.
|
||
|
|
* @link https://manaknightdigital.com
|
||
|
|
* @license Proprietary Software licensing
|
||
|
|
* @author Ryan Wong
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
const { ApolloError } = require('apollo-server-express');
|
||
|
|
const Sequelize = require('sequelize');
|
||
|
|
const { last } = require('lodash');
|
||
|
|
const graphqlFields = require('graphql-fields');
|
||
|
|
|
||
|
|
module.exports = async (_, { first, after }, { db, credential }, info) => {
|
||
|
|
//Check Auth if user allowed
|
||
|
|
try {
|
||
|
|
const attributes = db.refer_log.intersection(graphqlFields(info).edges.node);
|
||
|
|
|
||
|
|
const options = {
|
||
|
|
where: {},
|
||
|
|
limit: first,
|
||
|
|
attributes,
|
||
|
|
};
|
||
|
|
|
||
|
|
if (after) {
|
||
|
|
options.where = {
|
||
|
|
id: {
|
||
|
|
[Sequelize.Op.gt]: after,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const { count, rows } = await db.refer_log.findAndCountAll(options);
|
||
|
|
|
||
|
|
const edges = rows.map((refer_log) => ({
|
||
|
|
cursor: refer_log.id,
|
||
|
|
node: refer_log,
|
||
|
|
}));
|
||
|
|
|
||
|
|
const pageInfo = {
|
||
|
|
endCursor: last(edges)?.cursor,
|
||
|
|
hasNextPage: 0 < count - first,
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
edges,
|
||
|
|
pageInfo,
|
||
|
|
};
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.log('refer_log -> error', error);
|
||
|
|
return new ApolloError('InternalServerError');
|
||
|
|
}
|
||
|
|
}
|