Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: code to define success response schema(#85zrhhcgr) #47

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { events, Product, Response, Stock, Order, OrderItem, OrderPart, OPERATOR, User } from '@/types'
import { events, Product, Response, Stock, ListResponse, Order, OrderItem, OrderPart, OPERATOR, User } from '@/types'
import { init, resetConfig, updateToken, updateInstanceUrl } from '@/api'
import { isError } from '@/util'
import { fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStockAtFacility, getOrderDetails, getProfile, updateOrderStatus } from '@/modules'
Expand All @@ -17,6 +17,7 @@ export {
fetchProductsStockAtFacility,
getProfile,
events,
ListResponse,
Product,
Response,
Stock,
Expand Down
44 changes: 22 additions & 22 deletions src/modules/product/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import api from "@/api";
import { OPERATOR, Product, Response } from "@/types";
import { hasError, isError } from "@/util";
import { OPERATOR, Product, Response, ListResponse } from "@/types";
import { hasError } from "@/util";
import { transform } from 'node-json-transform'
import { productTransformRule } from "@/mappings/product";

async function fetchProducts(params: any): Promise<any | Response> {
async function fetchProducts(params: any): Promise<ListResponse<Product> | Response> {

const payload = {
"json": {
Expand Down Expand Up @@ -51,17 +51,17 @@ async function fetchProducts(params: any): Promise<any | Response> {

if (resp.status == 200 && !hasError(resp) && resp.data?.response?.numFound > 0) {

const product: Array<Product> = transform(resp.data.response.docs, productTransformRule)
const products: Array<Product> = transform(resp.data.response.docs, productTransformRule)

return {
products: product,
return Promise.resolve({
list: products,
total: resp.data?.response?.numFound
}
})
} else {
return {
products: {},
return Promise.resolve({
list: [],
total: 0
}
})
}
} catch (err) {
return Promise.reject({
Expand All @@ -72,7 +72,7 @@ async function fetchProducts(params: any): Promise<any | Response> {
}
}

async function fetchProductsGroupedBy(params: any): Promise<any | Response> {
async function fetchProductsGroupedBy(params: any): Promise<ListResponse<Product> | Response> {

const payload = {
"json": {
Expand Down Expand Up @@ -132,17 +132,17 @@ async function fetchProductsGroupedBy(params: any): Promise<any | Response> {
}
})

return {
products,
matches: resp.data?.grouped?.groupId.matches,
ngroups: resp.data?.grouped?.groupId.ngroups
}
return Promise.resolve({
list: products,
total: resp.data?.grouped?.groupId.matches,
groups: resp.data?.grouped?.groupId.ngroups
})
} else {
return {
products: {},
matches: 0,
ngroups: 0
}
return Promise.resolve({
list: [],
total: 0,
groups: 0
})
}
} catch (err) {
return Promise.reject({
Expand All @@ -153,7 +153,7 @@ async function fetchProductsGroupedBy(params: any): Promise<any | Response> {
}
}

async function fetchProductsGroupedByParent(params: any): Promise<Product[] | Response> {
async function fetchProductsGroupedByParent(params: any): Promise<ListResponse<Product> | Response> {

const payload = {
...params,
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ interface Response {
serverResponse?: any;
}

interface ListResponse<T> {
list: Array<T>;
total?: number;
groups?: number;
}

export {
ContactMech,
Enumeration,
events,
Geo,
ListResponse,
Order,
OrderItem,
OrderPart,
Expand Down