Added POST, DELETE and GET (all)

This commit is contained in:
Peter Morton 2023-11-06 23:45:47 -06:00
parent 4973a80f64
commit 816e0a1381

View File

@ -16,17 +16,62 @@ router.get("/", async (req, res, next) => {
); );
return; return;
} }
const { host, tenant, token } = auth;
logger.debug(`tps GET all properties from ${host}`);
axios
.get(
`${host}/tenant-properties-service/${tenant}/properties?fields=name,value,lastModifiedDate,lastModifiedBy`,
{
headers: {
Authorization: `OIDC_id_token ${token}`,
"Content-Type": "application/json",
},
}
)
.then((result) => {
if (result.data.errors && result.data.errors.length > 0) {
result.data.errors.forEach(function (error) {
logger.error("ERROR: Errors in results - " + error.message);
});
next(new Error("Error(s) getting properties "));
return;
}
const totalItems = result.data["hydra:totalItems"];
logger.debug("tps result has hydra:totalItems [" + totalItems + "]");
if (totalItems > 0) {
const data = result.data["hydra:member"].map(function (member) {
return {
name: member["vcfg:name"],
value: member["vcfg:value"],
lastModifiedDate: member["vcfg:lastModifiedDate"],
lastModifiedBy: member["vcfg:lastModifiedBy"],
};
});
res.json({ data: data });
} else {
res.json({ date: [] });
}
});
});
const propertyName = req.query.propertyName; router.get("/:name", async (req, res, next) => {
const auth = await store.get(req.query.authKey);
if (isEmpty(auth)) {
next(
new createHttpError.Forbidden(
`No authenication information found in store`
)
);
return;
}
const propertyName = req.params.name;
const { host, tenant, token } = auth; const { host, tenant, token } = auth;
logger.debug(`tps GET ${propertyName} from ${host}`); logger.debug(`tps GET ${propertyName} from ${host}`);
if (!propertyName || propertyName.length == 0) { if (!propertyName || propertyName.length == 0) {
next( next(new createHttpError.NotFound(`Blank property keys not allowed`));
new createHttpError.NotFound(
`Blank property keys not allowed`
)
);
return; return;
} }
@ -51,8 +96,10 @@ router.get("/", async (req, res, next) => {
const totalItems = result.data["hydra:totalItems"]; const totalItems = result.data["hydra:totalItems"];
logger.debug("tps result has hydra:totalItems [" + totalItems + "]"); logger.debug("tps result has hydra:totalItems [" + totalItems + "]");
if (totalItems > 0) { if (totalItems > 0) {
const value = result.data["hydra:member"][0]["vcfg:value"]; const member = result.data["hydra:member"][0];
res.json({ data: { value: value } }); res.json({
data: { name: member["vcfg:name"], value: member["vcfg:value"] },
});
} else { } else {
next( next(
new createHttpError.NotFound(`property ${propertyName} not found`) new createHttpError.NotFound(`property ${propertyName} not found`)
@ -61,4 +108,101 @@ router.get("/", async (req, res, next) => {
}); });
}); });
router.post("/", async (req, res, next) => {
const auth = await store.get(req.query.authKey);
if (isEmpty(auth)) {
next(
new createHttpError.Forbidden(
`No authenication information found in store`
)
);
return;
}
const { host, tenant, token } = auth;
const property = req.body.data;
const body = `[
{
"@type" : "vcfg:PropertyUpdateOrCreate",
"vcfg:name" :"${property.name}",
"vcfg:value" : "${property.value}"
}
]`;
logger.debug(`tps PATCH ${body} to ${host}`);
axios
.patch(`${host}/tenant-properties-service/${tenant}/properties`, body, {
headers: {
Authorization: `OIDC_id_token ${token}`,
"Content-Type": "application/ld+json",
},
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
logger.error(JSON.stringify(error.response.data));
logger.error(JSON.stringify(error.response.status));
logger.error(JSON.stringify(error.response.headers));
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser
// and an instance of http.ClientRequest in node.js
logger.error(JSON.stringify(error.request));
} else {
// Something happened in setting up the request that triggered an Error
logger.error("Error", error.message);
}
});
});
router.delete("/:name", async (req, res, next) => {
const auth = await store.get(req.query.authKey);
if (isEmpty(auth)) {
next(
new createHttpError.Forbidden(
`No authenication information found in store`
)
);
return;
}
const { host, tenant, token } = auth;
const propertyName = req.params.name;
const body = `[
{
"@type" : "vcfg:PropertyDelete",
"vcfg:name" : "${propertyName}"
}
]`;
logger.debug(`tps PATCH ${body} to ${host}`);
axios
.patch(`${host}/tenant-properties-service/${tenant}/properties`, body, {
headers: {
Authorization: `OIDC_id_token ${token}`,
"Content-Type": "application/ld+json",
},
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
logger.error(JSON.stringify(error.response.data));
logger.error(JSON.stringify(error.response.status));
logger.error(JSON.stringify(error.response.headers));
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser
// and an instance of http.ClientRequest in node.js
logger.error(JSON.stringify(error.request));
} else {
// Something happened in setting up the request that triggered an Error
logger.error("Error", error.message);
}
});
});
export default router; export default router;