Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #64 from Tolfix/dev
Browse files Browse the repository at this point in the history
1.5
  • Loading branch information
Tolfx authored Jan 8, 2022
2 parents a8591ac + 2927a92 commit 3f9879c
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpg",
"version": "v1.4",
"version": "v1.5",
"description": "CPG",
"main": "./build/Main.js",
"dependencies": {
Expand Down
368 changes: 367 additions & 1 deletion src/Database/GraphQL/Resolvers/Customer/Customer.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ObjectTypeComposer, ResolverResolveParams, Resolver, schemaComposer } from "graphql-compose";
import { ICustomer } from "../../../../Interfaces/Customer";
import CustomerModel from "../../../Models/Customers/Customer";
import InvoiceModel from "../../../Models/Invoices";
import OrderModel from "../../../Models/Orders";
import TransactionsModel from "../../../Models/Transactions";

export function myProfileResolver(
object: ObjectTypeComposer
Expand All @@ -23,7 +26,7 @@ export function myProfileResolver(
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
}, TArgs>) => {
} & TContext, TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

Expand All @@ -34,4 +37,367 @@ export function myProfileResolver(
return data;
}
})
};

export function myInvoicesResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myInvoices",
type: object.getType(),
args: {},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const invoices = await InvoiceModel.find({
$or: [
{ customer_uid: customer.uid},
{ customer_uid: customer.id}
]
});

return invoices;
}
})
};

export function myInvoiceResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myInvoice",
type: object.getType(),
args: {
id: "String!"
},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, {
id: unknown
} & TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

if(!args.id)
throw new Error(`Invoice ID is required`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const invoice = await InvoiceModel.findOne({
$or: [
{
customer_uid: customer.uid,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
{
customer_uid: customer.id,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
],
});

return invoice;
}
})
};

export function myOrdersResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myOrders",
type: object.getType(),
args: {},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const orders = await OrderModel.find({
$or: [
{ customer_uid: customer.uid},
{ customer_uid: customer.id}
]
});

return orders;
}
})
};

export function myOrderResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myInvoice",
type: object.getType(),
args: {
id: "String!"
},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, {
id: unknown
} & TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

if(!args.id)
throw new Error(`Order ID is required`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const order = await OrderModel.findOne({
$or: [
{
customer_uid: customer.uid,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
{
customer_uid: customer.id,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
],
});

return order;
}
})
};

export function myTransactionsResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myOrders",
type: object.getType(),
args: {},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const transactions = await TransactionsModel.find({
$or: [
{ customer_uid: customer.uid},
{ customer_uid: customer.id}
]
});

return transactions;
}
})
};

export function myTransactionResolver(
object: ObjectTypeComposer
): Resolver<any, any, any, any>
{
return schemaComposer.createResolver({
name: "myInvoice",
type: object.getType(),
args: {
id: "String!"
},
resolve: async <TSource, TContext, TArgs>({
source,
args,
context,
info,
projection,
}: ResolverResolveParams<TSource, {
isAdmin: boolean;
isUser: boolean;
userData: {
id: ICustomer["id"],
email: ICustomer["personal"]["email"],
}
} & TContext, {
id: unknown
} & TArgs>) => {
if(context.isAdmin)
throw new Error(`Admin not allowed to access this`);

if(!args.id)
throw new Error(`Transaction ID is required`);

const customer = await CustomerModel.findOne({
id: context.userData.id,
});

if(!customer)
throw new Error(`Customer not found`);

const transaction = await TransactionsModel.findOne({
$or: [
{
customer_uid: customer.uid,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
{
customer_uid: customer.id,
$or: [
{
id: args.id,
},
{
uid: args.id,
},
{
_id: args.id,
}
]
},
],
});

return transaction;
}
})
};
Loading

0 comments on commit 3f9879c

Please sign in to comment.