API - radio specifics
API for radio are built as described for web part in API This page only describes the Radio specifics.
Structure
Radio API files are organized in the following way:
pages/api/[name of the Family]/[name of the function]/index.ts
An API is mainly workflow-dedicated, but can be common to 2 processes. As examples:
ValidatePalletMoveFormcallsvalidatePalletMoveAPI that processes several mutations:pages/api/stock-management/validatePalletMove/index.tsimport { gql, GraphQLClient } from 'graphql-request';
import type { NextApiRequest, NextApiResponse } from 'next';
const parseCookie = (str: string) =>
str
.split(';')
.map((v) => v.split('='))
.reduce((acc: any, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
export default async (req: NextApiRequest, res: NextApiResponse) => {
const cookie = parseCookie(req.headers.cookie ?? '');
const token = cookie['token'];
const requestHeader = {
authorization: `Bearer ${token}`
};
const graphqlRequestClient = new GraphQLClient(
process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT as string,
{
headers: requestHeader
}
);
// retrieve information from front
const { handlingUnit, finalLocation } = req.body;
const movementCodes = {
[...]
};
//update hu section
const updateHUMutation = gql`
mutation updateHandlingUnit($id: String!, $input: UpdateHandlingUnitInput!) {
updateHandlingUnit(id: $id, input: $input) {
id
locationId
}
}
';
const updateHUVariables = {
id: handlingUnit.id,
input: { locationId: handlingUnit.locationId }
};
const updateHuResult = await graphqlRequestClient.request(
updateHUMutation,
updateHUVariables,
requestHeader
);
//end handling unit update section
//movements creation section
const createMovement = gql`
mutation createMovement($input: CreateMovementInput!) {
createMovement(input: $input) {
id
}
}
`;
let movementResults: any = [];
handlingUnit.handlingUnitContent.forEach(async (handlingUnitContent: any) => {
const movementVariables = {
input: {
originalLocationId: handlingUnit.locationId,
originalHandlingUnitId: handlingUnit.id,
originalContentId: handlingUnitContent.id,
articleId: handlingUnitContent.articleId,
stockOwnerId: handlingUnit.stockOwnerId,
...movementCodes,
finalLocationId: finalLocation.id,
finalContentId: handlingUnitContent.id,
finalHandlingUnitId: handlingUnit.id
}
};
const resultMovementLine = await graphqlRequestClient.request(
createMovement,
movementVariables,
requestHeader
);
movementResults.push(resultMovementLine);
});
//end movement creation section
//merge results
res.status(200).json({
response: {
updatedHU: updateHuResult,
movement: movementResults
}
});
};CheckFinalLocationPalletandCheckFinalLocationQuantityboth callcheckFinalLocationAPI that has the role of directing to the relevant back-end querypages/api/stock-management/checkFinalLocation/index.tsimport { gql, GraphQLClient } from "graphql-request";
import type { NextApiRequest, NextApiResponse } from "next";
const parseCookie = (str: string) =>
str
.split(';')
.map((v) => v.split('='))
.reduce((acc: any, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
export default async (req: NextApiRequest, res: NextApiResponse) => {
const cookie = parseCookie(req.headers.cookie ?? '');
const token = cookie['token'];
const requestHeader = {
authorization: `Bearer ${token}`
};
const graphqlRequestClient = new GraphQLClient(
process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT as string,
{
headers: requestHeader
}
);
const checkFinalLocationVariables = {
originLocationId: req.body.originLocationId,
destinationLocationId: req.body.destinationLocationId,
articleId: req.body.articleId
};
let checkFinalLocation: any | null;
if (req.body.movementType == 'quantityMvt') {
checkFinalLocation = gql`
query checkFinalLocationQuantity(
$originLocationId: String!
$destinationLocationId: String!
$articleId: String
) {
checkFinalLocationQuantity(
originLocationId: $originLocationId
destinationLocationId: $destinationLocationId
articleId: $articleId
) {
isCorrect
locationComparison
destinationContentId
destinationContentQuantity
destinationHuId
}
}
`;
} else if (req.body.movementType == 'palletMvt') {
checkFinalLocation = gql`
query checkFinalLocationPallet(
$originLocationId: String!
$destinationLocationId: String!
) {
checkFinalLocationPallet(
originLocationId: $originLocationId
destinationLocationId: $destinationLocationId
) {
isCorrect
locationComparison
}
}
`;
}
const result = await graphqlRequestClient
.request(checkFinalLocation, checkFinalLocationVariables, requestHeader)
.catch((error: any) => {
if (error.response.errors[0].extensions) {
res.status(500).json({
error: {
is_error: error.response.errors[0].extensions.is_error,
code: error.response.errors[0].extensions.code,
message: error.response.errors[0].message,
variables: error.response.errors[0].extensions.variables,
exception: error.response.errors[0].extensions.exception
}
});
} else {
res.status(500).json({ error });
}
});
if (result) {
res.status(200).json({ response: result });
}