Skip to content

Commit

Permalink
feat: assert datalayer connection to use api
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 8, 2022
1 parent 54a1cc6 commit dc3e35a
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 139 deletions.
149 changes: 86 additions & 63 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
assertHomeOrgExists,
assetNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
} from '../utils/data-assertions';

import { createProjectRecordsFromCsv } from '../utils/csv-utils';
Expand All @@ -45,6 +46,7 @@ export const create = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertDataLayerAvailable();

const newRecord = _.cloneDeep(req.body);
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -107,85 +109,103 @@ export const create = async (req, res) => {
};

export const findAll = async (req, res) => {
let { page, limit, search, orgUid, columns, xls } = req.query;
let where = orgUid ? { orgUid } : undefined;
try {
await assertDataLayerAvailable();
let { page, limit, search, orgUid, columns, xls } = req.query;
let where = orgUid ? { orgUid } : undefined;

const includes = Project.getAssociatedModels();

if (columns) {
// Remove any unsupported columns
columns = columns.filter((col) =>
Project.defaultColumns
.concat(includes.map((model) => model.name + 's'))
.includes(col),
);
} else {
columns = Project.defaultColumns.concat(
includes.map((model) => model.name + 's'),
);
}

const includes = Project.getAssociatedModels();
// If only FK fields have been specified, select just ID
if (!columns.length) {
columns = ['warehouseProjectId'];
}

if (columns) {
// Remove any unsupported columns
columns = columns.filter((col) =>
Project.defaultColumns
.concat(includes.map((model) => model.name + 's'))
.includes(col),
);
} else {
columns = Project.defaultColumns.concat(
includes.map((model) => model.name + 's'),
);
}
let results;
let pagination = paginationParams(page, limit);

// If only FK fields have been specified, select just ID
if (!columns.length) {
columns = ['warehouseProjectId'];
}
if (xls) {
pagination = { page: undefined, limit: undefined };
}

let results;
let pagination = paginationParams(page, limit);
if (search) {
results = await Project.fts(search, orgUid, pagination, columns);
}

if (xls) {
pagination = { page: undefined, limit: undefined };
}
if (!results) {
const query = {
...columnsToInclude(columns, includes),
...pagination,
};

results = await Project.findAndCountAll({
distinct: true,
where,
...query,
});
}

const response = optionallyPaginatedResponse(results, page, limit);

if (search) {
results = await Project.fts(search, orgUid, pagination, columns);
if (!xls) {
return res.json(response);
} else {
return sendXls(
Project.name,
createXlsFromSequelizeResults(response, Project, false, false, true),
res,
);
}
} catch (error) {
res.status(400).json({
message: 'Error retrieving projects',
error: error.message,
});
}
};

export const findOne = async (req, res) => {
try {
await assertDataLayerAvailable();

if (!results) {
const query = {
...columnsToInclude(columns, includes),
...pagination,
where: { warehouseProjectId: req.query.warehouseProjectId },
include: [
ProjectLocation,
Label,
Issuance,
CoBenefit,
RelatedProject,
Rating,
Estimation,
],
};

results = await Project.findAndCountAll({
distinct: true,
where,
...query,
res.json(await Project.findOne(query));
} catch (error) {
res.status(400).json({
message: 'Error retrieving projects',
error: error.message,
});
}

const response = optionallyPaginatedResponse(results, page, limit);

if (!xls) {
return res.json(response);
} else {
return sendXls(
Project.name,
createXlsFromSequelizeResults(response, Project, false, false, true),
res,
);
}
};

export const findOne = async (req, res) => {
const query = {
where: { warehouseProjectId: req.query.warehouseProjectId },
include: [
ProjectLocation,
Label,
Issuance,
CoBenefit,
RelatedProject,
Rating,
Estimation,
],
};

res.json(await Project.findOne(query));
};

export const updateFromXLS = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();

Expand Down Expand Up @@ -215,8 +235,9 @@ export const updateFromXLS = async (req, res) => {

export const update = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
// await assetNoPendingCommits();
await assetNoPendingCommits();

const originalRecord = await assertProjectRecordExists(
req.body.warehouseProjectId,
Expand Down Expand Up @@ -303,6 +324,7 @@ export const update = async (req, res) => {

export const destroy = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();

Expand Down Expand Up @@ -333,6 +355,7 @@ export const destroy = async (req, res) => {

export const batchUpload = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();

Expand Down
Loading

0 comments on commit dc3e35a

Please sign in to comment.