Your IP : 216.73.216.220


Current Path : /home/deltalab/PMS/partner-manager-backend/graphql/schema/
Upload File :
Current File : //home/deltalab/PMS/partner-manager-backend/graphql/schema/invoice.schema.js

const auth = require('../../services/auth');
const { invoiceTC } = require('../types/invoice.type');
const billing = require('../../services/billing');

// RESOLVERS =========================================
/*
Generate an invoice based on the provided partner id and time period
*/
invoiceTC.addResolver({
  kind: 'mutation',
  name: 'invoiceCreateOne',
  type: invoiceTC,
  args: {
    partnerId: { type: 'MongoID!' },
    startDate: { type: 'Date!' },
    endDate: { type: 'Date!' },
  },
  resolve: async ({ args }) => {
    const invoice = await billing.createInvoice(args.partnerId, args.startDate, args.endDate);
    const savedInvoice = await invoice.save();
    return savedInvoice;
  },
});

/*
  Get the invoices generated in the specified period of time, between the start date and the end date
*/
invoiceTC.addResolver({
  kind: 'query',
  name: 'invoicesPeriod',
  type: [invoiceTC],
  args: {
    partnerId: { type: 'MongoID!' },
    startDate: { type: 'Date!' },
    endDate: { type: 'Date!' },
  },
  resolve: async ({ args }) => {
    const invoices = await billing.fetchInvoicesPeriod(args.partnerId, args.startDate, args.endDate);
    return invoices;
  },
});

// QUERIES ===========================================
const invoiceQuery = {
  ...auth.authenticationRequired({
    invoiceById: invoiceTC.mongooseResolvers.findById(),
    invoiceOne: invoiceTC.mongooseResolvers.findOne(),
    invoiceMany: invoiceTC.mongooseResolvers.findMany(),
    invoiceCount: invoiceTC.mongooseResolvers.count(),
    invoicesPeriod: invoiceTC.getResolver('invoicesPeriod'),
  }),
};

// MUTATIONS =========================================
const invoiceMutation = {
  ...auth.authenticationRequired({
    invoiceGenerate: invoiceTC.getResolver('invoiceCreateOne'),
  }),
};

// EXPORTS ===========================================
module.exports = {
  invoiceQuery,
  invoiceMutation,
};