Initial version
This commit is contained in:
commit
6283e4b8ce
305
.cursorrules
Normal file
305
.cursorrules
Normal file
@ -0,0 +1,305 @@
|
||||
# Cursor Rules - Group 1: Development Philosophy & Coding Conventions
|
||||
1. Overall Architecture & Structure:
|
||||
- Enforce a clear separation of concerns between the backend and the frontend:
|
||||
- **Backend**: Use Express for routing, Passport for authentication, and Swagger for API documentation. Organize code into modules such as routes, services, and helpers.
|
||||
- **Example**:
|
||||
- Routes: `src/routes/auth.js` for authentication routes.
|
||||
- Services: `src/services/auth.js` for authentication logic.
|
||||
- Helpers: `src/helpers/wrapAsync.js` for wrapping asynchronous functions.
|
||||
- **Frontend**: Use Next.js with React and TypeScript. Structure components using functional components, hooks, and layouts.
|
||||
- **Example**:
|
||||
- Pages: `pages/index.tsx` for the main page.
|
||||
- Components: `components/Header.tsx` for the header component.
|
||||
- Layouts: `layouts/MainLayout.tsx` for common page layouts.
|
||||
- Ensure that backend modules and frontend components are organized for reusability and maintainability:
|
||||
- **Backend**: Separate business logic into services and use middleware for common tasks.
|
||||
- **Frontend**: Use reusable components and hooks to manage state and lifecycle.
|
||||
|
||||
2. Coding Style & Formatting:
|
||||
- For the backend (JavaScript):
|
||||
• Use ES6+ features (const/let, arrow functions) consistently.
|
||||
• Follow Prettier and ESLint configurations (e.g., consistent 2-space indentation, semicolons, and single quotes).
|
||||
• Maintain clear asynchronous patterns with helper wrappers (e.g., wrapAsync).
|
||||
- **Example from auth.js**:
|
||||
```javascript
|
||||
router.post('/signin/local', wrapAsync(async (req, res) => {
|
||||
const payload = await AuthService.signin(req.body.email, req.body.password, req);
|
||||
res.status(200).send(payload);
|
||||
}));
|
||||
```
|
||||
• Document API endpoints with inline Swagger comments to ensure API clarity and consistency.
|
||||
- **Example**:
|
||||
```javascript
|
||||
/**
|
||||
* @swagger
|
||||
* /api/auth/signin:
|
||||
* post:
|
||||
* summary: Sign in a user
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Successful login
|
||||
*/
|
||||
```
|
||||
- For the frontend (TypeScript/React):
|
||||
• Use functional components with strict typing and separation of concerns.
|
||||
- **Example**:
|
||||
```typescript
|
||||
const Button: React.FC<{ onClick: () => void }> = ({ onClick }) => (
|
||||
<button onClick={onClick}>Click me</button>
|
||||
);
|
||||
```
|
||||
• Follow naming conventions: PascalCase for components and types/interfaces, camelCase for variables, hooks, and function names.
|
||||
- **Example**:
|
||||
```typescript
|
||||
const useCustomHook = () => {
|
||||
const [state, setState] = useState(false);
|
||||
return [state, setState];
|
||||
};
|
||||
```
|
||||
• Utilize hooks (useEffect, useState) to manage state and lifecycle in a clear and concise manner.
|
||||
- **Example**:
|
||||
```typescript
|
||||
useEffect(() => {
|
||||
console.log('Component mounted');
|
||||
}, []);
|
||||
```
|
||||
|
||||
3. Code Quality & Best Practices:
|
||||
- Ensure code modularity by splitting complex logic into smaller, testable units.
|
||||
- **Example**: In `auth.js`, routes are separated from business logic, which is handled in `AuthService`.
|
||||
- Write self-documenting code and add comments where the logic is non-trivial.
|
||||
- **Example**: Use descriptive function and variable names in `auth.js`, and add comments for complex asynchronous operations.
|
||||
- Embrace declarative programming and adhere to SOLID principles.
|
||||
- **Example**: In service functions, ensure each function has a single responsibility and dependencies are injected rather than hardcoded.
|
||||
|
||||
4. Consistency & Tools Integration:
|
||||
- Leverage existing tools like Prettier and ESLint to automatically enforce style and formatting rules.
|
||||
- **Example**: Use `.prettierrc` and `.eslintrc.cjs` for configuration in your project.
|
||||
- Use TypeScript in the frontend to ensure type safety and catch errors early.
|
||||
- **Example**: Define interfaces and types in your React components to enforce strict typing.
|
||||
- Maintain uniformity in API design and error handling strategies.
|
||||
- **Example**: Consistently use Passport for authentication and a common error handling middleware in `auth.js`.
|
||||
|
||||
## Group 2 – Naming Conventions
|
||||
1. File Naming and Structure:
|
||||
• Frontend:
|
||||
- Page Files: Use lower-case filenames (e.g., index.tsx) as prescribed by Next.js conventions.
|
||||
- **Example**: `pages/index.tsx`, `pages/about.tsx`
|
||||
- Component Files: Use PascalCase for React component files (e.g., WebSiteHeader.tsx, NavBar.tsx).
|
||||
- **Example**: `components/Header.tsx`, `components/Footer.tsx`
|
||||
- Directories: Use clear, descriptive names (e.g., 'pages', 'components', 'WebPageComponents').
|
||||
- **Example**: `src/pages`, `src/components`
|
||||
• Backend:
|
||||
- Use lower-case filenames for modules (e.g., index.js, auth.js, projects.js).
|
||||
- **Example**: `routes/auth.js`, `services/user.js`
|
||||
- When needed, use hyphenation for clarity, but maintain consistency.
|
||||
- **Example**: `helpers/wrap-async.js`
|
||||
|
||||
2. Component and Module Naming:
|
||||
• Frontend:
|
||||
- React Components: Define components in PascalCase.
|
||||
- TypeScript Interfaces/Types: Use PascalCase (e.g., WebSiteHeaderProps).
|
||||
• Backend:
|
||||
- Classes (if any) and constructors should be in PascalCase; most helper functions and modules use camelCase.
|
||||
|
||||
3. Variable, Function, and Hook Naming:
|
||||
• Use camelCase for variables and function names in both frontend and backend.
|
||||
- **Example**:
|
||||
```javascript
|
||||
const userName = 'John Doe';
|
||||
function handleLogin() { ... }
|
||||
```
|
||||
• Custom Hooks: Prefix with 'use' (e.g., useAuth, useForm).
|
||||
- **Example**:
|
||||
```typescript
|
||||
const useAuth = () => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
return { isAuthenticated, setIsAuthenticated };
|
||||
};
|
||||
```
|
||||
|
||||
4. Consistency and Readability:
|
||||
• Maintain uniform naming across the project to ensure clarity and ease of maintenance.
|
||||
- **Example**: Use consistent naming conventions for variables, functions, and components, such as camelCase for variables and functions, and PascalCase for components.
|
||||
- **Example**: In `auth.js`, ensure that all function names clearly describe their purpose, such as `handleLogin` or `validateUserInput`.
|
||||
|
||||
## Group 3 – Frontend & React Best Practices
|
||||
1. Use of Functional Components & TypeScript:
|
||||
• Build all components as functional components.
|
||||
- **Example**:
|
||||
```typescript
|
||||
const Header: React.FC = () => {
|
||||
return <header>Header Content</header>;
|
||||
};
|
||||
```
|
||||
• Leverage TypeScript for static type checking and enforce strict prop and state types.
|
||||
- **Example**:
|
||||
```typescript
|
||||
interface ButtonProps {
|
||||
onClick: () => void;
|
||||
}
|
||||
const Button: React.FC<ButtonProps> = ({ onClick }) => (
|
||||
<button onClick={onClick}>Click me</button>
|
||||
);
|
||||
```
|
||||
|
||||
2. Effective Use of React Hooks:
|
||||
• Utilize useState and useEffect appropriately with proper dependency arrays.
|
||||
- **Example**:
|
||||
```typescript
|
||||
const [count, setCount] = useState(0);
|
||||
useEffect(() => {
|
||||
console.log('Component mounted');
|
||||
}, []);
|
||||
```
|
||||
• Create custom hooks to encapsulate shared logic (e.g., useAppSelector).
|
||||
- **Example**:
|
||||
```typescript
|
||||
const useAuth = () => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
return { isAuthenticated, setIsAuthenticated };
|
||||
};
|
||||
```
|
||||
|
||||
3. Component Composition & Separation of Concerns:
|
||||
• Separate presentational (stateless) components from container components managing logic.
|
||||
- **Example**: Use `LayoutGuest` to encapsulate common page structures.
|
||||
|
||||
4. Code Quality & Readability:
|
||||
• Maintain consistent formatting and adhere to Prettier and ESLint rules.
|
||||
• Use descriptive names for variables, functions, and components.
|
||||
• Document non-trivial logic with inline comments and consider implementing error boundaries where needed.
|
||||
• New code must adhere to these conventions to avoid ambiguity.
|
||||
• Use descriptive names that reflect the purpose and domain, avoiding abbreviations unless standard in the project.
|
||||
|
||||
## Group 4 – Backend & API Guidelines
|
||||
1. API Endpoint Design & Documentation:
|
||||
• Follow RESTful naming conventions; all route handlers should be named clearly and consistently.
|
||||
- **Example**: Use verbs like `GET`, `POST`, `PUT`, `DELETE` to define actions, e.g., `GET /api/auth/me` to retrieve user info.
|
||||
• Document endpoints with Swagger annotations to provide descriptions, expected request bodies, and response codes.
|
||||
- **Example**:
|
||||
```javascript
|
||||
/**
|
||||
* @swagger
|
||||
* /api/auth/signin:
|
||||
* post:
|
||||
* summary: Sign in a user
|
||||
* requestBody:
|
||||
* description: User credentials
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: "#/components/schemas/Auth"
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Successful login
|
||||
* 400:
|
||||
* description: Invalid username/password supplied
|
||||
*/
|
||||
```
|
||||
• Examples (for Auth endpoints):
|
||||
- POST /api/auth/signin/local
|
||||
• Description: Logs the user into the system.
|
||||
• Request Body (application/json):
|
||||
{ "email": "admin@flatlogic.com", "password": "password" }
|
||||
• Responses:
|
||||
- 200: Successful login (returns token and user data).
|
||||
- 400: Invalid username/password supplied.
|
||||
- GET /api/auth/me
|
||||
• Description: Retrieves current authorized user information.
|
||||
• Secured via Passport JWT; uses req.currentUser.
|
||||
• Responses:
|
||||
- 200: Returns current user info.
|
||||
- 400: Invalid credentials or missing user data.
|
||||
- POST /api/auth/signup
|
||||
• Description: Registers a new user.
|
||||
• Request Body (application/json):
|
||||
{ "email": "admin@flatlogic.com", "password": "password" }
|
||||
• Responses:
|
||||
- 200: New user signed up successfully.
|
||||
- 400: Invalid input supplied.
|
||||
- 500: Server error.
|
||||
|
||||
## Group 5 – Testing, Quality Assurance & Error Handling
|
||||
1. Testing Guidelines:
|
||||
• Write unit tests for critical backend and frontend components using frameworks such as Jest, React Testing Library, and Mocha/Chai.
|
||||
- **Example**:
|
||||
```javascript
|
||||
test('should return user data', async () => {
|
||||
const user = await getUserData();
|
||||
expect(user).toHaveProperty('email');
|
||||
});
|
||||
```
|
||||
• Practice test-driven development and maintain high test coverage.
|
||||
• Regularly update tests following changes in business logic.
|
||||
|
||||
2. Quality Assurance:
|
||||
• Enforce code quality with ESLint, Prettier, and static analysis tools.
|
||||
• Integrate continuous testing workflows (CI/CD) to catch issues early.
|
||||
- **Example**: Use GitHub Actions for automated testing and deployment.
|
||||
• Ensure documentation is kept up-to-date with the implemented code.
|
||||
|
||||
3. Error Handling:
|
||||
• Back-end:
|
||||
- Wrap asynchronous route handlers with a helper (e.g., wrapAsync) to capture errors.
|
||||
- **Example**:
|
||||
```javascript
|
||||
router.post('/signin', wrapAsync(async (req, res) => {
|
||||
const user = await AuthService.signin(req.body);
|
||||
res.send(user);
|
||||
}));
|
||||
```
|
||||
- Use centralized error handling middleware (e.g., commonErrorHandler) for uniform error responses.
|
||||
• Front-end:
|
||||
- Implement error boundaries in React to gracefully handle runtime errors.
|
||||
- Display user-friendly error messages and log errors for further analysis.
|
||||
|
||||
2. Authentication & Security:
|
||||
• Protect endpoints by using Passport.js with JWT (e.g., passport.authenticate('jwt', { session: false })).
|
||||
- **Example**:
|
||||
```javascript
|
||||
router.get('/profile', passport.authenticate('jwt', { session: false }), (req, res) => {
|
||||
res.send(req.user);
|
||||
});
|
||||
```
|
||||
• Ensure that secure routes check for existence of req.currentUser. If absent, return a ForbiddenError.
|
||||
|
||||
3. Consistent Error Handling & Middleware Usage:
|
||||
• Wrap asynchronous route handlers with helpers like wrapAsync for error propagation.
|
||||
• Use centralized error handling middleware (e.g., commonErrorHandler) to capture and format errors uniformly.
|
||||
|
||||
4. Modular Code Organization:
|
||||
• Organize backend code into separate files for routes, services, and database access (e.g., auth.js, projects.js, tasks.js).
|
||||
• Use descriptive, lowercase filenames for modules and routes.
|
||||
|
||||
5. Endpoint Security Best Practices:
|
||||
• Validate input data and sanitize requests where necessary.
|
||||
• Restrict sensitive operations to authenticated users with proper role-based permissions.
|
||||
|
||||
|
||||
────────────────────────────────────────
|
||||
Group 6 – Accessibility, UI, and Styling Guidelines (Updated)
|
||||
────────────────────────────────────────
|
||||
1. Sidebar Styling:
|
||||
• The sidebar is implemented in the authenticated layout via the AsideMenu component, with the actual element defined in AsideMenuLayer (located at frontend/src/components/AsideMenuLayer.tsx) as an <aside> element with id="asideMenu".
|
||||
- **Example**:
|
||||
```css
|
||||
#asideMenu {
|
||||
background-color: #F8F4E1 !important;
|
||||
}
|
||||
```
|
||||
• When modifying sidebar styles, target #asideMenu and its child elements rather than generic selectors (e.g., avoid .app-sidebar) to ensure that the changes affect the actual rendered sidebar.
|
||||
• Remove or override any conflicting background utilities (such as an unwanted bg-white) so our desired background color (#F8F4E1) is fully visible. Use a highly specific selector if necessary.
|
||||
• Adjust spacing (padding/margins) at both the container (#asideMenu) and the individual menu item level to maintain a consistent, compact design.
|
||||
|
||||
2. General Project Styling and Tailwind CSS Usage:
|
||||
• The application leverages Tailwind CSS extensively, with core styling defined in _theme.css using the @apply directive. Any new modifications should follow this pattern to ensure consistency.
|
||||
- **Example**:
|
||||
```css
|
||||
.btn {
|
||||
@apply bg-blue-500 text-white;
|
||||
}
|
||||
```
|
||||
• The themed blocks (like .theme-pink and .theme-green) standardize the UI's appearance. When applying custom overrides, ensure they integrate cleanly into these structures and avoid conflicts or circular dependency errors (e.g., issues when redefining utilities such as text-blue-600).
|
||||
• Adjustments via Tailwind CSS generally require modifying class names in the components and ensuring that global overrides are applied in the correct order. Consistent use of design tokens and custom color codes (e.g., #F8F4E1) throughout the app is crucial to a cohesive design.
|
||||
• Specificity is key. If a change isn't visually reflected as expected, inspect the rendered HTML to identify which classes are taking precedence.
|
||||
3
.dockerignore
Normal file
3
.dockerignore
Normal file
@ -0,0 +1,3 @@
|
||||
backend/node_modules
|
||||
frontend/node_modules
|
||||
frontend/build
|
||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
*/node_modules/
|
||||
*/build/
|
||||
187
502.html
Normal file
187
502.html
Normal file
@ -0,0 +1,187 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Service Starting</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
background-color: #EFF2FF;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
text-align: center;
|
||||
padding: 30px 40px;
|
||||
background-color: #fff;
|
||||
border-radius: 20px;
|
||||
margin-bottom: 20px;
|
||||
max-width: 538px;
|
||||
width: 100%;
|
||||
box-shadow: 0 13px 34px 0 rgba(167, 187, 242, 0.2);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#status-heading {
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
color: #02004E;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: #333;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
p {
|
||||
color: #666;
|
||||
font-size: 1.1em;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.tip {
|
||||
font-weight: 300;
|
||||
font-size: 17px;
|
||||
line-height: 150%;
|
||||
letter-spacing: 0;
|
||||
text-align: center;
|
||||
margin-top: 30px;
|
||||
}
|
||||
|
||||
.loader-container {
|
||||
position: relative;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.loader {
|
||||
width: 100px;
|
||||
aspect-ratio: 1;
|
||||
border-radius: 50%;
|
||||
background:
|
||||
radial-gradient(farthest-side, #5C7EF1 94%, #0000) top/8px 8px no-repeat,
|
||||
conic-gradient(#0000 30%, #5C7EF1);
|
||||
-webkit-mask: radial-gradient(farthest-side, #0000 calc(100% - 8px), #000 0);
|
||||
animation: l13 2s infinite linear;
|
||||
}
|
||||
|
||||
@keyframes l13 {
|
||||
100% {
|
||||
transform: rotate(1turn)
|
||||
}
|
||||
}
|
||||
|
||||
.app-logo {
|
||||
position: absolute;
|
||||
width: 36px;
|
||||
}
|
||||
|
||||
.panel {
|
||||
padding: 0 18px;
|
||||
display: none;
|
||||
background-color: white;
|
||||
overflow: hidden;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.project-info {
|
||||
border: 1px solid #8C9DFF;
|
||||
border-radius: 10px;
|
||||
padding: 12px 16px;
|
||||
max-width: 600px;
|
||||
margin: 40px auto;
|
||||
background-color: #FBFCFF;
|
||||
}
|
||||
|
||||
.project-info h2 {
|
||||
color: #02004E;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
margin-bottom: 10px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.project-info p {
|
||||
color: #686791;
|
||||
font-size: 12px;
|
||||
font-weight: 400;
|
||||
text-align: left;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2 id="status-heading">Loading the app, just a moment…</h2>
|
||||
<p class="tip">The application is currently launching. The page will automatically refresh once site is
|
||||
available.</p>
|
||||
<div class="project-info">
|
||||
<h2>Manufacturing ERP</h2>
|
||||
<p>Manufacturing ERP for production, materials, machines, QA and inventory with traceability and compliance audit trails.</p>
|
||||
</div>
|
||||
<div class="loader-container">
|
||||
<img src="https://flatlogic.com/blog/wp-content/uploads/2025/05/logo-bot-1.png" alt="App Logo"
|
||||
class="app-logo">
|
||||
<div class="loader"></div>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<video width="100%" height="315" controls loop>
|
||||
<source
|
||||
src="https://flatlogic.com/blog/wp-content/uploads/2025/04/20250430_1336_professional_dynamo_spinner_simple_compose_01jt349yvtenxt7xhg8hhr85j8.mp4"
|
||||
type="video/mp4">
|
||||
Your browser does not support the video tag.
|
||||
</video>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
function checkAvailability() {
|
||||
fetch('/')
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
window.location.reload();
|
||||
} else {
|
||||
setTimeout(checkAvailability, 5000);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
setTimeout(checkAvailability, 5000);
|
||||
});
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', checkAvailability);
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const appTitle = document.querySelector('#status-heading');
|
||||
const panel = document.querySelector('.panel');
|
||||
const video = panel.querySelector('video');
|
||||
let clickCount = 0;
|
||||
|
||||
appTitle.addEventListener('click', function () {
|
||||
clickCount++;
|
||||
if (clickCount === 5) {
|
||||
panel.classList.toggle('show');
|
||||
if (panel.classList.contains('show')) {
|
||||
video.play();
|
||||
} else {
|
||||
video.pause();
|
||||
}
|
||||
clickCount = 0;
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
21
Dockerfile
Normal file
21
Dockerfile
Normal file
@ -0,0 +1,21 @@
|
||||
FROM node:20.15.1-alpine AS builder
|
||||
RUN apk add --no-cache git
|
||||
WORKDIR /app
|
||||
COPY frontend/package.json frontend/yarn.lock ./
|
||||
RUN yarn install --pure-lockfile
|
||||
COPY frontend .
|
||||
RUN yarn build
|
||||
|
||||
|
||||
|
||||
FROM node:20.15.1-alpine
|
||||
WORKDIR /app
|
||||
COPY backend/package.json backend/yarn.lock ./
|
||||
RUN yarn install --pure-lockfile
|
||||
COPY backend .
|
||||
|
||||
COPY --from=builder /app/build /app/public
|
||||
CMD ["yarn", "start"]
|
||||
|
||||
|
||||
|
||||
85
Dockerfile.dev
Normal file
85
Dockerfile.dev
Normal file
@ -0,0 +1,85 @@
|
||||
# Base image for Node.js dependencies
|
||||
FROM node:20.15.1-alpine AS frontend-deps
|
||||
RUN apk add --no-cache git
|
||||
WORKDIR /app/frontend
|
||||
COPY frontend/package.json frontend/yarn.lock ./
|
||||
RUN yarn install --pure-lockfile
|
||||
|
||||
FROM node:20.15.1-alpine AS backend-deps
|
||||
RUN apk add --no-cache git
|
||||
WORKDIR /app/backend
|
||||
COPY backend/package.json backend/yarn.lock ./
|
||||
RUN yarn install --pure-lockfile
|
||||
|
||||
FROM node:20.15.1-alpine AS app-shell-deps
|
||||
RUN apk add --no-cache git
|
||||
WORKDIR /app/app-shell
|
||||
COPY app-shell/package.json app-shell/yarn.lock ./
|
||||
RUN yarn install --pure-lockfile
|
||||
|
||||
# Nginx setup and application build
|
||||
FROM node:20.15.1-alpine AS build
|
||||
RUN apk add --no-cache git nginx curl
|
||||
RUN apk add --no-cache lsof procps
|
||||
RUN yarn global add concurrently
|
||||
|
||||
RUN apk add --no-cache \
|
||||
chromium \
|
||||
nss \
|
||||
freetype \
|
||||
harfbuzz \
|
||||
ttf-freefont \
|
||||
fontconfig
|
||||
|
||||
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
|
||||
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
|
||||
|
||||
RUN mkdir -p /app/pids
|
||||
|
||||
# Make sure to add yarn global bin to PATH
|
||||
ENV PATH /root/.yarn/bin:/root/.config/yarn/global/node_modules/.bin:$PATH
|
||||
|
||||
# Copy dependencies
|
||||
WORKDIR /app
|
||||
COPY --from=frontend-deps /app/frontend /app/frontend
|
||||
COPY --from=backend-deps /app/backend /app/backend
|
||||
COPY --from=app-shell-deps /app/app-shell /app/app-shell
|
||||
|
||||
COPY frontend /app/frontend
|
||||
COPY backend /app/backend
|
||||
COPY app-shell /app/app-shell
|
||||
COPY docker /app/docker
|
||||
|
||||
# Copy all files from root to /app
|
||||
COPY . /app
|
||||
|
||||
# Copy Nginx configuration
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
||||
|
||||
# Copy custom error page
|
||||
COPY 502.html /usr/share/nginx/html/502.html
|
||||
|
||||
# Change owner and permissions of the error page
|
||||
RUN chown nginx:nginx /usr/share/nginx/html/502.html && \
|
||||
chmod 644 /usr/share/nginx/html/502.html
|
||||
|
||||
# Expose the port the app runs on
|
||||
EXPOSE 8080
|
||||
ENV NODE_ENV=dev_stage
|
||||
ENV FRONT_PORT=3001
|
||||
ENV BACKEND_PORT=3000
|
||||
ENV APP_SHELL_PORT=4000
|
||||
|
||||
|
||||
CMD ["sh", "-c", "\
|
||||
yarn --cwd /app/frontend dev & echo $! > /app/pids/frontend.pid && \
|
||||
yarn --cwd /app/backend start & echo $! > /app/pids/backend.pid && \
|
||||
sleep 10 && nginx -g 'daemon off;' & \
|
||||
NGINX_PID=$! && \
|
||||
echo 'Waiting for backend (port 3000) to be available...' && \
|
||||
while ! nc -z localhost ${BACKEND_PORT}; do \
|
||||
sleep 2; \
|
||||
done && \
|
||||
echo 'Backend is up. Starting app_shell for Git check...' && \
|
||||
yarn --cwd /app/app-shell start && \
|
||||
wait $NGINX_PID"]
|
||||
244
README.md
Normal file
244
README.md
Normal file
@ -0,0 +1,244 @@
|
||||
|
||||
|
||||
# Manufacturing ERP
|
||||
|
||||
|
||||
## This project was generated by [Flatlogic Platform](https://flatlogic.com).
|
||||
|
||||
|
||||
- Frontend: [React.js](https://flatlogic.com/templates?framework%5B%5D=react&sort=default)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- Backend: [NodeJS](https://flatlogic.com/templates?backend%5B%5D=nodejs&sort=default)
|
||||
|
||||
<details><summary>Backend Folder Structure</summary>
|
||||
|
||||
The generated application has the following backend folder structure:
|
||||
|
||||
`src` folder which contains your working files that will be used later to create the build. The src folder contains folders as:
|
||||
|
||||
- `auth` - config the library for authentication and authorization;
|
||||
|
||||
- `db` - contains such folders as:
|
||||
|
||||
- `api` - documentation that is automatically generated by jsdoc or other tools;
|
||||
|
||||
- `migrations` - is a skeleton of the database or all the actions that users do with the database;
|
||||
|
||||
- `models`- what will represent the database for the backend;
|
||||
|
||||
- `seeders` - the entity that creates the data for the database.
|
||||
|
||||
- `routes` - this folder would contain all the routes that you have created using Express Router and what they do would be exported from a Controller file;
|
||||
|
||||
- `services` - contains such folders as `emails` and `notifications`.
|
||||
</details>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- Database: PostgreSQL
|
||||
|
||||
|
||||
- app-shel: Core application framework that provides essential infrastructure services
|
||||
for the entire application.
|
||||
-----------------------
|
||||
### We offer 2 ways how to start the project locally: by running Frontend and Backend or with Docker.
|
||||
-----------------------
|
||||
|
||||
## To start the project:
|
||||
|
||||
### Backend:
|
||||
|
||||
> Please change current folder: `cd backend`
|
||||
|
||||
|
||||
|
||||
#### Install local dependencies:
|
||||
`yarn install`
|
||||
|
||||
------------
|
||||
|
||||
#### Adjust local db:
|
||||
##### 1. Install postgres:
|
||||
|
||||
MacOS:
|
||||
|
||||
`brew install postgres`
|
||||
|
||||
> if you don’t have ‘brew‘ please install it (https://brew.sh) and repeat step `brew install postgres`.
|
||||
|
||||
Ubuntu:
|
||||
|
||||
`sudo apt update`
|
||||
|
||||
`sudo apt install postgresql postgresql-contrib`
|
||||
|
||||
##### 2. Create db and admin user:
|
||||
Before run and test connection, make sure you have created a database as described in the above configuration. You can use the `psql` command to create a user and database.
|
||||
|
||||
`psql postgres --u postgres`
|
||||
|
||||
Next, type this command for creating a new user with password then give access for creating the database.
|
||||
|
||||
`postgres-# CREATE ROLE admin WITH LOGIN PASSWORD 'admin_pass';`
|
||||
|
||||
`postgres-# ALTER ROLE admin CREATEDB;`
|
||||
|
||||
Quit `psql` then log in again using the new user that previously created.
|
||||
|
||||
`postgres-# \q`
|
||||
|
||||
`psql postgres -U admin`
|
||||
|
||||
Type this command to creating a new database.
|
||||
|
||||
`postgres=> CREATE DATABASE db_{your_project_name};`
|
||||
|
||||
Then give that new user privileges to the new database then quit the `psql`.
|
||||
|
||||
`postgres=> GRANT ALL PRIVILEGES ON DATABASE db_{your_project_name} TO admin;`
|
||||
|
||||
`postgres=> \q`
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
------------
|
||||
|
||||
|
||||
#### Create database:
|
||||
`yarn db:create`
|
||||
|
||||
#### Start production build:
|
||||
`yarn start`
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### Frontend:
|
||||
|
||||
> Please change current folder: `cd frontend`
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## To start the project with Docker:
|
||||
### Description:
|
||||
|
||||
The project contains the **docker folder** and the `Dockerfile`.
|
||||
|
||||
The `Dockerfile` is used to Deploy the project to Google Cloud.
|
||||
|
||||
The **docker folder** contains a couple of helper scripts:
|
||||
|
||||
- `docker-compose.yml` (all our services: web, backend, db are described here)
|
||||
- `start-backend.sh` (starts backend, but only after the database)
|
||||
- `wait-for-it.sh` (imported from https://github.com/vishnubob/wait-for-it)
|
||||
|
||||
> To avoid breaking the application, we recommend you don't edit the following files: everything that includes the **docker folder** and `Dokerfile`.
|
||||
|
||||
## Run services:
|
||||
|
||||
1. Install docker compose (https://docs.docker.com/compose/install/)
|
||||
|
||||
2. Move to `docker` folder. All next steps should be done from this folder.
|
||||
|
||||
``` cd docker ```
|
||||
|
||||
3. Make executables from `wait-for-it.sh` and `start-backend.sh`:
|
||||
|
||||
``` chmod +x start-backend.sh && chmod +x wait-for-it.sh ```
|
||||
|
||||
4. Download dependend projects for services.
|
||||
|
||||
5. Review the docker-compose.yml file. Make sure that all services have Dockerfiles. Only db service doesn't require a Dockerfile.
|
||||
|
||||
6. Make sure you have needed ports (see them in `ports`) available on your local machine.
|
||||
|
||||
7. Start services:
|
||||
|
||||
7.1. With an empty database `rm -rf data && docker-compose up`
|
||||
|
||||
7.2. With a stored (from previus runs) database data `docker-compose up`
|
||||
|
||||
8. Check http://localhost:3000
|
||||
|
||||
9. Stop services:
|
||||
|
||||
9.1. Just press `Ctr+C`
|
||||
|
||||
## Most common errors:
|
||||
|
||||
1. `connection refused`
|
||||
|
||||
There could be many reasons, but the most common are:
|
||||
|
||||
- The port is not open on the destination machine.
|
||||
|
||||
- The port is open on the destination machine, but its backlog of pending connections is full.
|
||||
|
||||
- A firewall between the client and server is blocking access (also check local firewalls).
|
||||
|
||||
After checking for firewalls and that the port is open, use telnet to connect to the IP/port to test connectivity. This removes any potential issues from your application.
|
||||
|
||||
***MacOS:***
|
||||
|
||||
If you suspect that your SSH service might be down, you can run this command to find out:
|
||||
|
||||
`sudo service ssh status`
|
||||
|
||||
If the command line returns a status of down, then you’ve likely found the reason behind your connectivity error.
|
||||
|
||||
***Ubuntu:***
|
||||
|
||||
Sometimes a connection refused error can also indicate that there is an IP address conflict on your network. You can search for possible IP conflicts by running:
|
||||
|
||||
`arp-scan -I eth0 -l | grep <ipaddress>`
|
||||
|
||||
`arp-scan -I eth0 -l | grep <ipaddress>`
|
||||
|
||||
and
|
||||
|
||||
`arping <ipaddress>`
|
||||
|
||||
2. `yarn db:create` creates database with the assembled tables (on MacOS with Postgres database)
|
||||
|
||||
The workaround - put the next commands to your Postgres database terminal:
|
||||
|
||||
`DROP SCHEMA public CASCADE;`
|
||||
|
||||
`CREATE SCHEMA public;`
|
||||
|
||||
`GRANT ALL ON SCHEMA public TO postgres;`
|
||||
|
||||
`GRANT ALL ON SCHEMA public TO public;`
|
||||
|
||||
Afterwards, continue to start your project in the backend directory by running:
|
||||
|
||||
`yarn start`
|
||||
14
backend/.env
Normal file
14
backend/.env
Normal file
@ -0,0 +1,14 @@
|
||||
DB_NAME=app_39565
|
||||
DB_USER=app_39565
|
||||
DB_PASS=16f79e22-9365-4f3e-9940-99161e2afa21
|
||||
DB_HOST=127.0.0.1
|
||||
DB_PORT=5432
|
||||
PORT=3000
|
||||
GOOGLE_CLIENT_ID=671001533244-kf1k1gmp6mnl0r030qmvdu6v36ghmim6.apps.googleusercontent.com
|
||||
GOOGLE_CLIENT_SECRET=Yo4qbKZniqvojzUQ60iKlxqR
|
||||
MS_CLIENT_ID=4696f457-31af-40de-897c-e00d7d4cff73
|
||||
MS_CLIENT_SECRET=m8jzZ.5UpHF3=-dXzyxiZ4e[F8OF54@p
|
||||
EMAIL_USER=AKIAVEW7G4PQUBGM52OF
|
||||
EMAIL_PASS=BLnD4hKGb6YkSz3gaQrf8fnyLi3C3/EdjOOsLEDTDPTz
|
||||
SECRET_KEY=HUEyqESqgQ1yTwzVlO6wprC9Kf1J1xuA
|
||||
PEXELS_KEY=Vc99rnmOhHhJAbgGQoKLZtsaIVfkeownoQNbTj78VemUjKh08ZYRbf18
|
||||
4
backend/.eslintignore
Normal file
4
backend/.eslintignore
Normal file
@ -0,0 +1,4 @@
|
||||
# Ignore generated and runtime files
|
||||
node_modules/
|
||||
tmp/
|
||||
logs/
|
||||
15
backend/.eslintrc.cjs
Normal file
15
backend/.eslintrc.cjs
Normal file
@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
node: true,
|
||||
es2021: true
|
||||
},
|
||||
extends: [
|
||||
'eslint:recommended'
|
||||
],
|
||||
plugins: [
|
||||
'import'
|
||||
],
|
||||
rules: {
|
||||
'import/no-unresolved': 'error'
|
||||
}
|
||||
};
|
||||
11
backend/.prettierrc
Normal file
11
backend/.prettierrc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2,
|
||||
"printWidth": 80,
|
||||
"trailingComma": "all",
|
||||
"quoteProps": "as-needed",
|
||||
"jsxSingleQuote": true,
|
||||
"bracketSpacing": true,
|
||||
"bracketSameLine": false,
|
||||
"arrowParens": "always"
|
||||
}
|
||||
7
backend/.sequelizerc
Normal file
7
backend/.sequelizerc
Normal file
@ -0,0 +1,7 @@
|
||||
const path = require('path');
|
||||
module.exports = {
|
||||
"config": path.resolve("src", "db", "db.config.js"),
|
||||
"models-path": path.resolve("src", "db", "models"),
|
||||
"seeders-path": path.resolve("src", "db", "seeders"),
|
||||
"migrations-path": path.resolve("src", "db", "migrations")
|
||||
};
|
||||
23
backend/Dockerfile
Normal file
23
backend/Dockerfile
Normal file
@ -0,0 +1,23 @@
|
||||
FROM node:20.15.1-alpine
|
||||
|
||||
RUN apk update && apk add bash
|
||||
# Create app directory
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Install app dependencies
|
||||
# A wildcard is used to ensure both package.json AND package-lock.json are copied
|
||||
# where available (npm@5+)
|
||||
COPY package*.json ./
|
||||
|
||||
RUN yarn install
|
||||
# If you are building your code for production
|
||||
# RUN npm ci --only=production
|
||||
|
||||
|
||||
# Bundle app source
|
||||
COPY . .
|
||||
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
CMD [ "yarn", "start" ]
|
||||
56
backend/README.md
Normal file
56
backend/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
#Manufacturing ERP - template backend,
|
||||
|
||||
#### Run App on local machine:
|
||||
|
||||
##### Install local dependencies:
|
||||
- `yarn install`
|
||||
|
||||
------------
|
||||
|
||||
##### Adjust local db:
|
||||
###### 1. Install postgres:
|
||||
- MacOS:
|
||||
- `brew install postgres`
|
||||
|
||||
- Ubuntu:
|
||||
- `sudo apt update`
|
||||
- `sudo apt install postgresql postgresql-contrib`
|
||||
|
||||
###### 2. Create db and admin user:
|
||||
- Before run and test connection, make sure you have created a database as described in the above configuration. You can use the `psql` command to create a user and database.
|
||||
- `psql postgres --u postgres`
|
||||
|
||||
- Next, type this command for creating a new user with password then give access for creating the database.
|
||||
- `postgres-# CREATE ROLE admin WITH LOGIN PASSWORD 'admin_pass';`
|
||||
- `postgres-# ALTER ROLE admin CREATEDB;`
|
||||
|
||||
- Quit `psql` then log in again using the new user that previously created.
|
||||
- `postgres-# \q`
|
||||
- `psql postgres -U admin`
|
||||
|
||||
- Type this command to creating a new database.
|
||||
- `postgres=> CREATE DATABASE db_manufacturing_erp;`
|
||||
|
||||
- Then give that new user privileges to the new database then quit the `psql`.
|
||||
- `postgres=> GRANT ALL PRIVILEGES ON DATABASE db_manufacturing_erp TO admin;`
|
||||
- `postgres=> \q`
|
||||
|
||||
------------
|
||||
|
||||
#### Api Documentation (Swagger)
|
||||
|
||||
http://localhost:8080/api-docs (local host)
|
||||
|
||||
http://host_name/api-docs
|
||||
|
||||
------------
|
||||
|
||||
##### Setup database tables or update after schema change
|
||||
- `yarn db:migrate`
|
||||
|
||||
##### Seed the initial data (admin accounts, relevant for the first setup):
|
||||
- `yarn db:seed`
|
||||
|
||||
##### Start build:
|
||||
- `yarn start`
|
||||
56
backend/package.json
Normal file
56
backend/package.json
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "manufacturingerp",
|
||||
"description": "Manufacturing ERP - template backend",
|
||||
"scripts": {
|
||||
"start": "npm run db:migrate && npm run db:seed && npm run watch",
|
||||
"lint": "eslint . --ext .js",
|
||||
"db:migrate": "sequelize-cli db:migrate",
|
||||
"db:seed": "sequelize-cli db:seed:all",
|
||||
"db:drop": "sequelize-cli db:drop",
|
||||
"db:create": "sequelize-cli db:create",
|
||||
"watch": "node watcher.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@google-cloud/storage": "^5.18.2",
|
||||
"axios": "^1.6.7",
|
||||
"bcrypt": "5.1.1",
|
||||
"chokidar": "^4.0.3",
|
||||
"cors": "2.8.5",
|
||||
"csv-parser": "^3.0.0",
|
||||
"express": "4.18.2",
|
||||
"formidable": "1.2.2",
|
||||
"helmet": "4.1.1",
|
||||
"json2csv": "^5.0.7",
|
||||
"jsonwebtoken": "8.5.1",
|
||||
"lodash": "4.17.21",
|
||||
"moment": "2.30.1",
|
||||
"multer": "^1.4.4",
|
||||
"mysql2": "2.2.5",
|
||||
"nodemailer": "6.9.9",
|
||||
"passport": "^0.7.0",
|
||||
"passport-google-oauth2": "^0.2.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-microsoft": "^0.1.0",
|
||||
"pg": "8.4.1",
|
||||
"pg-hstore": "2.3.4",
|
||||
"sequelize": "6.35.2",
|
||||
"sequelize-json-schema": "^2.1.1",
|
||||
"sqlite": "4.0.15",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.0",
|
||||
"tedious": "^18.2.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"cross-env": "7.0.3",
|
||||
"eslint": "^8.23.1",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"mocha": "8.1.3",
|
||||
"node-mocks-http": "1.9.0",
|
||||
"nodemon": "2.0.5",
|
||||
"sequelize-cli": "6.6.2"
|
||||
}
|
||||
}
|
||||
484
backend/src/ai/LocalAIApi.js
Normal file
484
backend/src/ai/LocalAIApi.js
Normal file
@ -0,0 +1,484 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const http = require("http");
|
||||
const https = require("https");
|
||||
const { URL } = require("url");
|
||||
|
||||
let CONFIG_CACHE = null;
|
||||
|
||||
class LocalAIApi {
|
||||
static createResponse(params, options) {
|
||||
return createResponse(params, options);
|
||||
}
|
||||
|
||||
static request(pathValue, payload, options) {
|
||||
return request(pathValue, payload, options);
|
||||
}
|
||||
|
||||
static fetchStatus(aiRequestId, options) {
|
||||
return fetchStatus(aiRequestId, options);
|
||||
}
|
||||
|
||||
static awaitResponse(aiRequestId, options) {
|
||||
return awaitResponse(aiRequestId, options);
|
||||
}
|
||||
|
||||
static extractText(response) {
|
||||
return extractText(response);
|
||||
}
|
||||
|
||||
static decodeJsonFromResponse(response) {
|
||||
return decodeJsonFromResponse(response);
|
||||
}
|
||||
}
|
||||
|
||||
async function createResponse(params, options = {}) {
|
||||
const payload = { ...(params || {}) };
|
||||
|
||||
if (!Array.isArray(payload.input) || payload.input.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
error: "input_missing",
|
||||
message: 'Parameter "input" is required and must be a non-empty array.',
|
||||
};
|
||||
}
|
||||
|
||||
const cfg = config();
|
||||
if (!payload.model) {
|
||||
payload.model = cfg.defaultModel;
|
||||
}
|
||||
|
||||
const initial = await request(options.path, payload, options);
|
||||
if (!initial.success) {
|
||||
return initial;
|
||||
}
|
||||
|
||||
const data = initial.data;
|
||||
if (data && typeof data === "object" && data.ai_request_id) {
|
||||
const pollTimeout = Number(options.poll_timeout ?? 300);
|
||||
const pollInterval = Number(options.poll_interval ?? 5);
|
||||
return await awaitResponse(data.ai_request_id, {
|
||||
interval: pollInterval,
|
||||
timeout: pollTimeout,
|
||||
headers: options.headers,
|
||||
timeout_per_call: options.timeout,
|
||||
verify_tls: options.verify_tls,
|
||||
});
|
||||
}
|
||||
|
||||
return initial;
|
||||
}
|
||||
|
||||
async function request(pathValue, payload = {}, options = {}) {
|
||||
const cfg = config();
|
||||
const resolvedPath = pathValue || options.path || cfg.responsesPath;
|
||||
|
||||
if (!resolvedPath) {
|
||||
return {
|
||||
success: false,
|
||||
error: "project_id_missing",
|
||||
message: "PROJECT_ID is not defined; cannot resolve AI proxy endpoint.",
|
||||
};
|
||||
}
|
||||
|
||||
if (!cfg.projectUuid) {
|
||||
return {
|
||||
success: false,
|
||||
error: "project_uuid_missing",
|
||||
message: "PROJECT_UUID is not defined; aborting AI request.",
|
||||
};
|
||||
}
|
||||
|
||||
const bodyPayload = { ...(payload || {}) };
|
||||
if (!bodyPayload.project_uuid) {
|
||||
bodyPayload.project_uuid = cfg.projectUuid;
|
||||
}
|
||||
|
||||
const url = buildUrl(resolvedPath, cfg.baseUrl);
|
||||
const timeout = resolveTimeout(options.timeout, cfg.timeout);
|
||||
const verifyTls = resolveVerifyTls(options.verify_tls, cfg.verifyTls);
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
[cfg.projectHeader]: cfg.projectUuid,
|
||||
};
|
||||
if (Array.isArray(options.headers)) {
|
||||
for (const header of options.headers) {
|
||||
if (typeof header === "string" && header.includes(":")) {
|
||||
const [name, value] = header.split(":", 2);
|
||||
headers[name.trim()] = value.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const body = JSON.stringify(bodyPayload);
|
||||
return sendRequest(url, "POST", body, headers, timeout, verifyTls);
|
||||
}
|
||||
|
||||
async function fetchStatus(aiRequestId, options = {}) {
|
||||
const cfg = config();
|
||||
if (!cfg.projectUuid) {
|
||||
return {
|
||||
success: false,
|
||||
error: "project_uuid_missing",
|
||||
message: "PROJECT_UUID is not defined; aborting status check.",
|
||||
};
|
||||
}
|
||||
|
||||
const statusPath = resolveStatusPath(aiRequestId, cfg);
|
||||
const url = buildUrl(statusPath, cfg.baseUrl);
|
||||
const timeout = resolveTimeout(options.timeout, cfg.timeout);
|
||||
const verifyTls = resolveVerifyTls(options.verify_tls, cfg.verifyTls);
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json",
|
||||
[cfg.projectHeader]: cfg.projectUuid,
|
||||
};
|
||||
if (Array.isArray(options.headers)) {
|
||||
for (const header of options.headers) {
|
||||
if (typeof header === "string" && header.includes(":")) {
|
||||
const [name, value] = header.split(":", 2);
|
||||
headers[name.trim()] = value.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sendRequest(url, "GET", null, headers, timeout, verifyTls);
|
||||
}
|
||||
|
||||
async function awaitResponse(aiRequestId, options = {}) {
|
||||
const timeout = Number(options.timeout ?? 300);
|
||||
const interval = Math.max(Number(options.interval ?? 5), 1);
|
||||
const deadline = Date.now() + Math.max(timeout, interval) * 1000;
|
||||
|
||||
while (true) {
|
||||
const statusResp = await fetchStatus(aiRequestId, {
|
||||
headers: options.headers,
|
||||
timeout: options.timeout_per_call,
|
||||
verify_tls: options.verify_tls,
|
||||
});
|
||||
|
||||
if (statusResp.success) {
|
||||
const data = statusResp.data || {};
|
||||
if (data && typeof data === "object") {
|
||||
if (data.status === "success") {
|
||||
return {
|
||||
success: true,
|
||||
status: 200,
|
||||
data: data.response || data,
|
||||
};
|
||||
}
|
||||
if (data.status === "failed") {
|
||||
return {
|
||||
success: false,
|
||||
status: 500,
|
||||
error: String(data.error || "AI request failed"),
|
||||
data,
|
||||
};
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return statusResp;
|
||||
}
|
||||
|
||||
if (Date.now() >= deadline) {
|
||||
return {
|
||||
success: false,
|
||||
error: "timeout",
|
||||
message: "Timed out waiting for AI response.",
|
||||
};
|
||||
}
|
||||
|
||||
await sleep(interval * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function extractText(response) {
|
||||
const payload = response && typeof response === "object" ? response.data || response : null;
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (Array.isArray(payload.output)) {
|
||||
let combined = "";
|
||||
for (const item of payload.output) {
|
||||
if (!item || !Array.isArray(item.content)) {
|
||||
continue;
|
||||
}
|
||||
for (const block of item.content) {
|
||||
if (
|
||||
block &&
|
||||
typeof block === "object" &&
|
||||
block.type === "output_text" &&
|
||||
typeof block.text === "string" &&
|
||||
block.text.length > 0
|
||||
) {
|
||||
combined += block.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (combined) {
|
||||
return combined;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
payload.choices &&
|
||||
payload.choices[0] &&
|
||||
payload.choices[0].message &&
|
||||
typeof payload.choices[0].message.content === "string"
|
||||
) {
|
||||
return payload.choices[0].message.content;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function decodeJsonFromResponse(response) {
|
||||
const text = extractText(response);
|
||||
if (!text) {
|
||||
throw new Error("No text found in AI response.");
|
||||
}
|
||||
|
||||
const parsed = parseJson(text);
|
||||
if (parsed.ok && parsed.value && typeof parsed.value === "object") {
|
||||
return parsed.value;
|
||||
}
|
||||
|
||||
const stripped = stripJsonFence(text);
|
||||
if (stripped !== text) {
|
||||
const parsedStripped = parseJson(stripped);
|
||||
if (parsedStripped.ok && parsedStripped.value && typeof parsedStripped.value === "object") {
|
||||
return parsedStripped.value;
|
||||
}
|
||||
throw new Error(`JSON parse failed after stripping fences: ${parsedStripped.error}`);
|
||||
}
|
||||
|
||||
throw new Error(`JSON parse failed: ${parsed.error}`);
|
||||
}
|
||||
|
||||
function config() {
|
||||
if (CONFIG_CACHE) {
|
||||
return CONFIG_CACHE;
|
||||
}
|
||||
|
||||
ensureEnvLoaded();
|
||||
|
||||
const baseUrl = process.env.AI_PROXY_BASE_URL || "https://flatlogic.com";
|
||||
const projectId = process.env.PROJECT_ID || null;
|
||||
let responsesPath = process.env.AI_RESPONSES_PATH || null;
|
||||
if (!responsesPath && projectId) {
|
||||
responsesPath = `/projects/${projectId}/ai-request`;
|
||||
}
|
||||
|
||||
const timeout = resolveTimeout(process.env.AI_TIMEOUT, 30);
|
||||
const verifyTls = resolveVerifyTls(process.env.AI_VERIFY_TLS, true);
|
||||
|
||||
CONFIG_CACHE = {
|
||||
baseUrl,
|
||||
responsesPath,
|
||||
projectId,
|
||||
projectUuid: process.env.PROJECT_UUID || null,
|
||||
projectHeader: process.env.AI_PROJECT_HEADER || "project-uuid",
|
||||
defaultModel: process.env.AI_DEFAULT_MODEL || "gpt-5-mini",
|
||||
timeout,
|
||||
verifyTls,
|
||||
};
|
||||
|
||||
return CONFIG_CACHE;
|
||||
}
|
||||
|
||||
function buildUrl(pathValue, baseUrl) {
|
||||
const trimmed = String(pathValue || "").trim();
|
||||
if (trimmed === "") {
|
||||
return baseUrl;
|
||||
}
|
||||
if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
|
||||
return trimmed;
|
||||
}
|
||||
if (trimmed.startsWith("/")) {
|
||||
return `${baseUrl}${trimmed}`;
|
||||
}
|
||||
return `${baseUrl}/${trimmed}`;
|
||||
}
|
||||
|
||||
function resolveStatusPath(aiRequestId, cfg) {
|
||||
const basePath = (cfg.responsesPath || "").replace(/\/+$/, "");
|
||||
if (!basePath) {
|
||||
return `/ai-request/${encodeURIComponent(String(aiRequestId))}/status`;
|
||||
}
|
||||
const normalized = basePath.endsWith("/ai-request") ? basePath : `${basePath}/ai-request`;
|
||||
return `${normalized}/${encodeURIComponent(String(aiRequestId))}/status`;
|
||||
}
|
||||
|
||||
function sendRequest(urlString, method, body, headers, timeoutSeconds, verifyTls) {
|
||||
return new Promise((resolve) => {
|
||||
let targetUrl;
|
||||
try {
|
||||
targetUrl = new URL(urlString);
|
||||
} catch (err) {
|
||||
resolve({
|
||||
success: false,
|
||||
error: "invalid_url",
|
||||
message: err.message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const isHttps = targetUrl.protocol === "https:";
|
||||
const requestFn = isHttps ? https.request : http.request;
|
||||
const options = {
|
||||
protocol: targetUrl.protocol,
|
||||
hostname: targetUrl.hostname,
|
||||
port: targetUrl.port || (isHttps ? 443 : 80),
|
||||
path: `${targetUrl.pathname}${targetUrl.search}`,
|
||||
method: method.toUpperCase(),
|
||||
headers,
|
||||
timeout: Math.max(Number(timeoutSeconds || 30), 1) * 1000,
|
||||
};
|
||||
if (isHttps) {
|
||||
options.rejectUnauthorized = Boolean(verifyTls);
|
||||
}
|
||||
|
||||
const req = requestFn(options, (res) => {
|
||||
let responseBody = "";
|
||||
res.setEncoding("utf8");
|
||||
res.on("data", (chunk) => {
|
||||
responseBody += chunk;
|
||||
});
|
||||
res.on("end", () => {
|
||||
const status = res.statusCode || 0;
|
||||
const parsed = parseJson(responseBody);
|
||||
const payload = parsed.ok ? parsed.value : responseBody;
|
||||
|
||||
if (status >= 200 && status < 300) {
|
||||
const result = {
|
||||
success: true,
|
||||
status,
|
||||
data: payload,
|
||||
};
|
||||
if (!parsed.ok) {
|
||||
result.json_error = parsed.error;
|
||||
}
|
||||
resolve(result);
|
||||
return;
|
||||
}
|
||||
|
||||
const errorMessage =
|
||||
parsed.ok && payload && typeof payload === "object"
|
||||
? String(payload.error || payload.message || "AI proxy request failed")
|
||||
: String(responseBody || "AI proxy request failed");
|
||||
|
||||
resolve({
|
||||
success: false,
|
||||
status,
|
||||
error: errorMessage,
|
||||
response: payload,
|
||||
json_error: parsed.ok ? undefined : parsed.error,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on("timeout", () => {
|
||||
req.destroy(new Error("request_timeout"));
|
||||
});
|
||||
|
||||
req.on("error", (err) => {
|
||||
resolve({
|
||||
success: false,
|
||||
error: "request_failed",
|
||||
message: err.message,
|
||||
});
|
||||
});
|
||||
|
||||
if (body) {
|
||||
req.write(body);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function parseJson(value) {
|
||||
if (typeof value !== "string" || value.trim() === "") {
|
||||
return { ok: false, error: "empty_response" };
|
||||
}
|
||||
try {
|
||||
return { ok: true, value: JSON.parse(value) };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
function stripJsonFence(text) {
|
||||
const trimmed = text.trim();
|
||||
if (trimmed.startsWith("```json")) {
|
||||
return trimmed.replace(/^```json/, "").replace(/```$/, "").trim();
|
||||
}
|
||||
if (trimmed.startsWith("```")) {
|
||||
return trimmed.replace(/^```/, "").replace(/```$/, "").trim();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function resolveTimeout(value, fallback) {
|
||||
const parsed = Number.parseInt(String(value ?? fallback), 10);
|
||||
return Number.isNaN(parsed) ? Number(fallback) : parsed;
|
||||
}
|
||||
|
||||
function resolveVerifyTls(value, fallback) {
|
||||
if (value === undefined || value === null) {
|
||||
return Boolean(fallback);
|
||||
}
|
||||
return String(value).toLowerCase() !== "false" && String(value) !== "0";
|
||||
}
|
||||
|
||||
function ensureEnvLoaded() {
|
||||
if (process.env.PROJECT_UUID && process.env.PROJECT_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
const envPath = path.resolve(__dirname, "../../../../.env");
|
||||
if (!fs.existsSync(envPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let content;
|
||||
try {
|
||||
content = fs.readFileSync(envPath, "utf8");
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to read executor .env: ${err.message}`);
|
||||
}
|
||||
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#") || !trimmed.includes("=")) {
|
||||
continue;
|
||||
}
|
||||
const [rawKey, ...rest] = trimmed.split("=");
|
||||
const key = rawKey.trim();
|
||||
if (!key) {
|
||||
continue;
|
||||
}
|
||||
const value = rest.join("=").trim().replace(/^['"]|['"]$/g, "");
|
||||
if (!process.env[key]) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LocalAIApi,
|
||||
createResponse,
|
||||
request,
|
||||
fetchStatus,
|
||||
awaitResponse,
|
||||
extractText,
|
||||
decodeJsonFromResponse,
|
||||
};
|
||||
68
backend/src/auth/auth.js
Normal file
68
backend/src/auth/auth.js
Normal file
@ -0,0 +1,68 @@
|
||||
const config = require('../config');
|
||||
const providers = config.providers;
|
||||
const helpers = require('../helpers');
|
||||
const db = require('../db/models');
|
||||
|
||||
const passport = require('passport');
|
||||
const JWTstrategy = require('passport-jwt').Strategy;
|
||||
const ExtractJWT = require('passport-jwt').ExtractJwt;
|
||||
const GoogleStrategy = require('passport-google-oauth2').Strategy;
|
||||
const MicrosoftStrategy = require('passport-microsoft').Strategy;
|
||||
const UsersDBApi = require('../db/api/users');
|
||||
|
||||
|
||||
passport.use(new JWTstrategy({
|
||||
passReqToCallback: true,
|
||||
secretOrKey: config.secret_key,
|
||||
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken()
|
||||
}, async (req, token, done) => {
|
||||
try {
|
||||
const user = await UsersDBApi.findBy( {email: token.user.email});
|
||||
|
||||
if (user && user.disabled) {
|
||||
return done (new Error(`User '${user.email}' is disabled`));
|
||||
}
|
||||
|
||||
req.currentUser = user;
|
||||
|
||||
return done(null, user);
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
}));
|
||||
|
||||
passport.use(new GoogleStrategy({
|
||||
clientID: config.google.clientId,
|
||||
clientSecret: config.google.clientSecret,
|
||||
callbackURL: config.apiUrl + '/auth/signin/google/callback',
|
||||
passReqToCallback: true
|
||||
},
|
||||
function (request, accessToken, refreshToken, profile, done) {
|
||||
socialStrategy(profile.email, profile, providers.GOOGLE, done);
|
||||
}
|
||||
));
|
||||
|
||||
|
||||
passport.use(new MicrosoftStrategy({
|
||||
clientID: config.microsoft.clientId,
|
||||
clientSecret: config.microsoft.clientSecret,
|
||||
callbackURL: config.apiUrl + '/auth/signin/microsoft/callback',
|
||||
passReqToCallback: true
|
||||
},
|
||||
function (request, accessToken, refreshToken, profile, done) {
|
||||
const email = profile._json.mail || profile._json.userPrincipalName;
|
||||
socialStrategy(email, profile, providers.MICROSOFT, done);
|
||||
}
|
||||
));
|
||||
|
||||
function socialStrategy(email, profile, provider, done) {
|
||||
db.users.findOrCreate({where: {email, provider}}).then(([user, created]) => {
|
||||
const body = {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: profile.displayName,
|
||||
};
|
||||
const token = helpers.jwtSign({user: body});
|
||||
return done(null, {token});
|
||||
});
|
||||
}
|
||||
79
backend/src/config.js
Normal file
79
backend/src/config.js
Normal file
@ -0,0 +1,79 @@
|
||||
|
||||
|
||||
|
||||
const os = require('os');
|
||||
|
||||
const config = {
|
||||
gcloud: {
|
||||
bucket: "fldemo-files",
|
||||
hash: "afeefb9d49f5b7977577876b99532ac7"
|
||||
},
|
||||
bcrypt: {
|
||||
saltRounds: 12
|
||||
},
|
||||
admin_pass: "16f79e22",
|
||||
user_pass: "99161e2afa21",
|
||||
admin_email: "admin@flatlogic.com",
|
||||
providers: {
|
||||
LOCAL: 'local',
|
||||
GOOGLE: 'google',
|
||||
MICROSOFT: 'microsoft'
|
||||
},
|
||||
secret_key: process.env.SECRET_KEY || '16f79e22-9365-4f3e-9940-99161e2afa21',
|
||||
remote: '',
|
||||
port: process.env.NODE_ENV === "production" ? "" : "8080",
|
||||
hostUI: process.env.NODE_ENV === "production" ? "" : "http://localhost",
|
||||
portUI: process.env.NODE_ENV === "production" ? "" : "3000",
|
||||
|
||||
portUIProd: process.env.NODE_ENV === "production" ? "" : ":3000",
|
||||
|
||||
swaggerUI: process.env.NODE_ENV === "production" ? "" : "http://localhost",
|
||||
swaggerPort: process.env.NODE_ENV === "production" ? "" : ":8080",
|
||||
google: {
|
||||
clientId: process.env.GOOGLE_CLIENT_ID || '',
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
|
||||
},
|
||||
microsoft: {
|
||||
clientId: process.env.MS_CLIENT_ID || '',
|
||||
clientSecret: process.env.MS_CLIENT_SECRET || '',
|
||||
},
|
||||
uploadDir: os.tmpdir(),
|
||||
email: {
|
||||
from: 'Manufacturing ERP <app@flatlogic.app>',
|
||||
host: 'email-smtp.us-east-1.amazonaws.com',
|
||||
port: 587,
|
||||
auth: {
|
||||
user: process.env.EMAIL_USER || '',
|
||||
pass: process.env.EMAIL_PASS,
|
||||
},
|
||||
tls: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
},
|
||||
roles: {
|
||||
|
||||
admin: 'Administrator',
|
||||
|
||||
|
||||
|
||||
user: 'Inventory Controller',
|
||||
|
||||
},
|
||||
|
||||
project_uuid: '16f79e22-9365-4f3e-9940-99161e2afa21',
|
||||
flHost: process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'dev_stage' ? 'https://flatlogic.com/projects' : 'http://localhost:3000/projects',
|
||||
|
||||
|
||||
gpt_key: process.env.GPT_KEY || '',
|
||||
};
|
||||
|
||||
config.pexelsKey = process.env.PEXELS_KEY || '';
|
||||
|
||||
config.pexelsQuery = 'Factory gears at sunrise';
|
||||
config.host = process.env.NODE_ENV === "production" ? config.remote : "http://localhost";
|
||||
config.apiUrl = `${config.host}${config.port ? `:${config.port}` : ``}/api`;
|
||||
config.swaggerUrl = `${config.swaggerUI}${config.swaggerPort}`;
|
||||
config.uiUrl = `${config.hostUI}${config.portUI ? `:${config.portUI}` : ``}/#`;
|
||||
config.backUrl = `${config.hostUI}${config.portUI ? `:${config.portUI}` : ``}`;
|
||||
|
||||
module.exports = config;
|
||||
550
backend/src/db/api/audit_events.js
Normal file
550
backend/src/db/api/audit_events.js
Normal file
@ -0,0 +1,550 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Audit_eventsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_events = await db.audit_events.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
event_name: data.event_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_type: data.event_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_at: data.event_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity_name: data.entity_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
record_reference: data.record_reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
details: data.details
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ip_address: data.ip_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await audit_events.setActor( data.actor || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const audit_eventsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
event_name: item.event_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_type: item.event_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_at: item.event_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity_name: item.entity_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
record_reference: item.record_reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
details: item.details
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ip_address: item.ip_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const audit_events = await db.audit_events.bulkCreate(audit_eventsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const audit_events = await db.audit_events.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.event_name !== undefined) updatePayload.event_name = data.event_name;
|
||||
|
||||
|
||||
if (data.event_type !== undefined) updatePayload.event_type = data.event_type;
|
||||
|
||||
|
||||
if (data.event_at !== undefined) updatePayload.event_at = data.event_at;
|
||||
|
||||
|
||||
if (data.entity_name !== undefined) updatePayload.entity_name = data.entity_name;
|
||||
|
||||
|
||||
if (data.record_reference !== undefined) updatePayload.record_reference = data.record_reference;
|
||||
|
||||
|
||||
if (data.details !== undefined) updatePayload.details = data.details;
|
||||
|
||||
|
||||
if (data.ip_address !== undefined) updatePayload.ip_address = data.ip_address;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await audit_events.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.actor !== undefined) {
|
||||
await audit_events.setActor(
|
||||
|
||||
data.actor,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_events = await db.audit_events.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of audit_events) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of audit_events) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_events = await db.audit_events.findByPk(id, options);
|
||||
|
||||
await audit_events.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await audit_events.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_events = await db.audit_events.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!audit_events) {
|
||||
return audit_events;
|
||||
}
|
||||
|
||||
const output = audit_events.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.actor = await audit_events.getActor({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'actor',
|
||||
|
||||
where: filter.actor ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.actor.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.actor.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.event_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_events',
|
||||
'event_name',
|
||||
filter.event_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.entity_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_events',
|
||||
'entity_name',
|
||||
filter.entity_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.record_reference) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_events',
|
||||
'record_reference',
|
||||
filter.record_reference,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.details) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_events',
|
||||
'details',
|
||||
filter.details,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.ip_address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_events',
|
||||
'ip_address',
|
||||
filter.ip_address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.event_atRange) {
|
||||
const [start, end] = filter.event_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
event_at: {
|
||||
...where.event_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
event_at: {
|
||||
...where.event_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.event_type) {
|
||||
where = {
|
||||
...where,
|
||||
event_type: filter.event_type,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.audit_events.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'audit_events',
|
||||
'event_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.audit_events.findAll({
|
||||
attributes: [ 'id', 'event_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['event_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.event_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
565
backend/src/db/api/bom_lines.js
Normal file
565
backend/src/db/api/bom_lines.js
Normal file
@ -0,0 +1,565 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Bom_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const bom_lines = await db.bom_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
quantity_per: data.quantity_per
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scrap_factor: data.scrap_factor
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_method: data.issue_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: data.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await bom_lines.setBom( data.bom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await bom_lines.setComponent_item( data.component_item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await bom_lines.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const bom_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
quantity_per: item.quantity_per
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scrap_factor: item.scrap_factor
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_method: item.issue_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: item.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const bom_lines = await db.bom_lines.bulkCreate(bom_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const bom_lines = await db.bom_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.quantity_per !== undefined) updatePayload.quantity_per = data.quantity_per;
|
||||
|
||||
|
||||
if (data.scrap_factor !== undefined) updatePayload.scrap_factor = data.scrap_factor;
|
||||
|
||||
|
||||
if (data.issue_method !== undefined) updatePayload.issue_method = data.issue_method;
|
||||
|
||||
|
||||
if (data.line_notes !== undefined) updatePayload.line_notes = data.line_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await bom_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.bom !== undefined) {
|
||||
await bom_lines.setBom(
|
||||
|
||||
data.bom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.component_item !== undefined) {
|
||||
await bom_lines.setComponent_item(
|
||||
|
||||
data.component_item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await bom_lines.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const bom_lines = await db.bom_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of bom_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of bom_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const bom_lines = await db.bom_lines.findByPk(id, options);
|
||||
|
||||
await bom_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await bom_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const bom_lines = await db.bom_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!bom_lines) {
|
||||
return bom_lines;
|
||||
}
|
||||
|
||||
const output = bom_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.bom = await bom_lines.getBom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.component_item = await bom_lines.getComponent_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await bom_lines.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.boms,
|
||||
as: 'bom',
|
||||
|
||||
where: filter.bom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.bom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
bom_name: {
|
||||
[Op.or]: filter.bom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'component_item',
|
||||
|
||||
where: filter.component_item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.component_item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.component_item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'bom_lines',
|
||||
'line_notes',
|
||||
filter.line_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.quantity_perRange) {
|
||||
const [start, end] = filter.quantity_perRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity_per: {
|
||||
...where.quantity_per,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity_per: {
|
||||
...where.quantity_per,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.scrap_factorRange) {
|
||||
const [start, end] = filter.scrap_factorRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scrap_factor: {
|
||||
...where.scrap_factor,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scrap_factor: {
|
||||
...where.scrap_factor,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.issue_method) {
|
||||
where = {
|
||||
...where,
|
||||
issue_method: filter.issue_method,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.bom_lines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'bom_lines',
|
||||
'line_notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.bom_lines.findAll({
|
||||
attributes: [ 'id', 'line_notes' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['line_notes', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.line_notes,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
571
backend/src/db/api/boms.js
Normal file
571
backend/src/db/api/boms.js
Normal file
@ -0,0 +1,571 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class BomsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const boms = await db.boms.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
bom_name: data.bom_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
bom_code: data.bom_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
bom_status: data.bom_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
revision: data.revision
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
effective_from: data.effective_from
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
effective_to: data.effective_to
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await boms.setParent_item( data.parent_item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return boms;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const bomsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
bom_name: item.bom_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
bom_code: item.bom_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
bom_status: item.bom_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
revision: item.revision
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
effective_from: item.effective_from
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
effective_to: item.effective_to
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const boms = await db.boms.bulkCreate(bomsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return boms;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const boms = await db.boms.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.bom_name !== undefined) updatePayload.bom_name = data.bom_name;
|
||||
|
||||
|
||||
if (data.bom_code !== undefined) updatePayload.bom_code = data.bom_code;
|
||||
|
||||
|
||||
if (data.bom_status !== undefined) updatePayload.bom_status = data.bom_status;
|
||||
|
||||
|
||||
if (data.revision !== undefined) updatePayload.revision = data.revision;
|
||||
|
||||
|
||||
if (data.effective_from !== undefined) updatePayload.effective_from = data.effective_from;
|
||||
|
||||
|
||||
if (data.effective_to !== undefined) updatePayload.effective_to = data.effective_to;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await boms.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.parent_item !== undefined) {
|
||||
await boms.setParent_item(
|
||||
|
||||
data.parent_item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return boms;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const boms = await db.boms.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of boms) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of boms) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return boms;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const boms = await db.boms.findByPk(id, options);
|
||||
|
||||
await boms.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await boms.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return boms;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const boms = await db.boms.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!boms) {
|
||||
return boms;
|
||||
}
|
||||
|
||||
const output = boms.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.bom_lines_bom = await boms.getBom_lines_bom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_bom = await boms.getWork_orders_bom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.parent_item = await boms.getParent_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'parent_item',
|
||||
|
||||
where: filter.parent_item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.parent_item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.parent_item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.bom_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'boms',
|
||||
'bom_name',
|
||||
filter.bom_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.bom_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'boms',
|
||||
'bom_code',
|
||||
filter.bom_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.revision) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'boms',
|
||||
'revision',
|
||||
filter.revision,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'boms',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.effective_fromRange) {
|
||||
const [start, end] = filter.effective_fromRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
effective_from: {
|
||||
...where.effective_from,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
effective_from: {
|
||||
...where.effective_from,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.effective_toRange) {
|
||||
const [start, end] = filter.effective_toRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
effective_to: {
|
||||
...where.effective_to,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
effective_to: {
|
||||
...where.effective_to,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.bom_status) {
|
||||
where = {
|
||||
...where,
|
||||
bom_status: filter.bom_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.boms.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'boms',
|
||||
'bom_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.boms.findAll({
|
||||
attributes: [ 'id', 'bom_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['bom_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.bom_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
534
backend/src/db/api/customers.js
Normal file
534
backend/src/db/api/customers.js
Normal file
@ -0,0 +1,534 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class CustomersDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const customers = await db.customers.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
customer_name: data.customer_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
customer_code: data.customer_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contact_name: data.contact_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: data.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: data.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_address: data.billing_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipping_address: data.shipping_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const customersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
customer_name: item.customer_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
customer_code: item.customer_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contact_name: item.contact_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: item.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: item.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_address: item.billing_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipping_address: item.shipping_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const customers = await db.customers.bulkCreate(customersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const customers = await db.customers.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.customer_name !== undefined) updatePayload.customer_name = data.customer_name;
|
||||
|
||||
|
||||
if (data.customer_code !== undefined) updatePayload.customer_code = data.customer_code;
|
||||
|
||||
|
||||
if (data.contact_name !== undefined) updatePayload.contact_name = data.contact_name;
|
||||
|
||||
|
||||
if (data.email !== undefined) updatePayload.email = data.email;
|
||||
|
||||
|
||||
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
||||
|
||||
|
||||
if (data.billing_address !== undefined) updatePayload.billing_address = data.billing_address;
|
||||
|
||||
|
||||
if (data.shipping_address !== undefined) updatePayload.shipping_address = data.shipping_address;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await customers.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const customers = await db.customers.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of customers) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of customers) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const customers = await db.customers.findByPk(id, options);
|
||||
|
||||
await customers.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await customers.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const customers = await db.customers.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!customers) {
|
||||
return customers;
|
||||
}
|
||||
|
||||
const output = customers.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_customer = await customers.getWork_orders_customer({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.shipments_customer = await customers.getShipments_customer({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.customer_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'customer_name',
|
||||
filter.customer_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.customer_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'customer_code',
|
||||
filter.customer_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.contact_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'contact_name',
|
||||
filter.contact_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'email',
|
||||
filter.email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'phone',
|
||||
filter.phone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.billing_address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'billing_address',
|
||||
filter.billing_address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.shipping_address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'customers',
|
||||
'shipping_address',
|
||||
filter.shipping_address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.customers.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'customers',
|
||||
'customer_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.customers.findAll({
|
||||
attributes: [ 'id', 'customer_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['customer_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.customer_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
87
backend/src/db/api/file.js
Normal file
87
backend/src/db/api/file.js
Normal file
@ -0,0 +1,87 @@
|
||||
const db = require('../models');
|
||||
const assert = require('assert');
|
||||
const services = require('../../services/file');
|
||||
|
||||
module.exports = class FileDBApi {
|
||||
static async replaceRelationFiles(
|
||||
relation,
|
||||
rawFiles,
|
||||
options,
|
||||
) {
|
||||
assert(relation.belongsTo, 'belongsTo is required');
|
||||
assert(
|
||||
relation.belongsToColumn,
|
||||
'belongsToColumn is required',
|
||||
);
|
||||
assert(relation.belongsToId, 'belongsToId is required');
|
||||
|
||||
let files = [];
|
||||
|
||||
if (Array.isArray(rawFiles)) {
|
||||
files = rawFiles;
|
||||
} else {
|
||||
files = rawFiles ? [rawFiles] : [];
|
||||
}
|
||||
|
||||
await this._removeLegacyFiles(relation, files, options);
|
||||
await this._addFiles(relation, files, options);
|
||||
}
|
||||
|
||||
static async _addFiles(relation, files, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
|
||||
const inexistentFiles = files.filter(
|
||||
(file) => !!file.new,
|
||||
);
|
||||
|
||||
for (const file of inexistentFiles) {
|
||||
await db.file.create(
|
||||
{
|
||||
belongsTo: relation.belongsTo,
|
||||
belongsToColumn: relation.belongsToColumn,
|
||||
belongsToId: relation.belongsToId,
|
||||
name: file.name,
|
||||
sizeInBytes: file.sizeInBytes,
|
||||
privateUrl: file.privateUrl,
|
||||
publicUrl: file.publicUrl,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static async _removeLegacyFiles(
|
||||
relation,
|
||||
files,
|
||||
options,
|
||||
) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const filesToDelete = await db.file.findAll({
|
||||
where: {
|
||||
belongsTo: relation.belongsTo,
|
||||
belongsToId: relation.belongsToId,
|
||||
belongsToColumn: relation.belongsToColumn,
|
||||
id: {
|
||||
[db.Sequelize.Op
|
||||
.notIn]: files
|
||||
.filter((file) => !file.new)
|
||||
.map((file) => file.id)
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
for (let file of filesToDelete) {
|
||||
await services.deleteGCloud(file.privateUrl);
|
||||
await file.destroy({
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
877
backend/src/db/api/inventory_transactions.js
Normal file
877
backend/src/db/api/inventory_transactions.js
Normal file
@ -0,0 +1,877 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Inventory_transactionsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const inventory_transactions = await db.inventory_transactions.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
transaction_number: data.transaction_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
transaction_at: data.transaction_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
transaction_type: data.transaction_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quantity: data.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_cost: data.unit_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reference: data.reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reason: data.reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await inventory_transactions.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setFrom_location( data.from_location || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setTo_location( data.to_location || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setQa_inspection( data.qa_inspection || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.setPerformed_by( data.performed_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.inventory_transactions.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: inventory_transactions.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const inventory_transactionsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
transaction_number: item.transaction_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
transaction_at: item.transaction_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
transaction_type: item.transaction_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quantity: item.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_cost: item.unit_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reference: item.reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reason: item.reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const inventory_transactions = await db.inventory_transactions.bulkCreate(inventory_transactionsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < inventory_transactions.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.inventory_transactions.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: inventory_transactions[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const inventory_transactions = await db.inventory_transactions.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.transaction_number !== undefined) updatePayload.transaction_number = data.transaction_number;
|
||||
|
||||
|
||||
if (data.transaction_at !== undefined) updatePayload.transaction_at = data.transaction_at;
|
||||
|
||||
|
||||
if (data.transaction_type !== undefined) updatePayload.transaction_type = data.transaction_type;
|
||||
|
||||
|
||||
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
|
||||
|
||||
|
||||
if (data.unit_cost !== undefined) updatePayload.unit_cost = data.unit_cost;
|
||||
|
||||
|
||||
if (data.reference !== undefined) updatePayload.reference = data.reference;
|
||||
|
||||
|
||||
if (data.reason !== undefined) updatePayload.reason = data.reason;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await inventory_transactions.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await inventory_transactions.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await inventory_transactions.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.from_location !== undefined) {
|
||||
await inventory_transactions.setFrom_location(
|
||||
|
||||
data.from_location,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.to_location !== undefined) {
|
||||
await inventory_transactions.setTo_location(
|
||||
|
||||
data.to_location,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await inventory_transactions.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await inventory_transactions.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.qa_inspection !== undefined) {
|
||||
await inventory_transactions.setQa_inspection(
|
||||
|
||||
data.qa_inspection,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.performed_by !== undefined) {
|
||||
await inventory_transactions.setPerformed_by(
|
||||
|
||||
data.performed_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.inventory_transactions.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: inventory_transactions.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const inventory_transactions = await db.inventory_transactions.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of inventory_transactions) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of inventory_transactions) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const inventory_transactions = await db.inventory_transactions.findByPk(id, options);
|
||||
|
||||
await inventory_transactions.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await inventory_transactions.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const inventory_transactions = await db.inventory_transactions.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!inventory_transactions) {
|
||||
return inventory_transactions;
|
||||
}
|
||||
|
||||
const output = inventory_transactions.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.item = await inventory_transactions.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await inventory_transactions.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.from_location = await inventory_transactions.getFrom_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.to_location = await inventory_transactions.getTo_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await inventory_transactions.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.work_order = await inventory_transactions.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.qa_inspection = await inventory_transactions.getQa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.performed_by = await inventory_transactions.getPerformed_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await inventory_transactions.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.locations,
|
||||
as: 'from_location',
|
||||
|
||||
where: filter.from_location ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.from_location.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
location_name: {
|
||||
[Op.or]: filter.from_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.locations,
|
||||
as: 'to_location',
|
||||
|
||||
where: filter.to_location ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.to_location.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
location_name: {
|
||||
[Op.or]: filter.to_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.qa_inspections,
|
||||
as: 'qa_inspection',
|
||||
|
||||
where: filter.qa_inspection ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.qa_inspection.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
inspection_number: {
|
||||
[Op.or]: filter.qa_inspection.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'performed_by',
|
||||
|
||||
where: filter.performed_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.performed_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.performed_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.transaction_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'inventory_transactions',
|
||||
'transaction_number',
|
||||
filter.transaction_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.reference) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'inventory_transactions',
|
||||
'reference',
|
||||
filter.reference,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.reason) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'inventory_transactions',
|
||||
'reason',
|
||||
filter.reason,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.transaction_atRange) {
|
||||
const [start, end] = filter.transaction_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
transaction_at: {
|
||||
...where.transaction_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
transaction_at: {
|
||||
...where.transaction_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.quantityRange) {
|
||||
const [start, end] = filter.quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.unit_costRange) {
|
||||
const [start, end] = filter.unit_costRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_cost: {
|
||||
...where.unit_cost,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_cost: {
|
||||
...where.unit_cost,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.transaction_type) {
|
||||
where = {
|
||||
...where,
|
||||
transaction_type: filter.transaction_type,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.inventory_transactions.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'inventory_transactions',
|
||||
'transaction_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.inventory_transactions.findAll({
|
||||
attributes: [ 'id', 'transaction_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['transaction_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.transaction_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
434
backend/src/db/api/item_categories.js
Normal file
434
backend/src/db/api/item_categories.js
Normal file
@ -0,0 +1,434 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Item_categoriesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const item_categories = await db.item_categories.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
category_name: data.category_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
category_code: data.category_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const item_categoriesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
category_name: item.category_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
category_code: item.category_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const item_categories = await db.item_categories.bulkCreate(item_categoriesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const item_categories = await db.item_categories.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.category_name !== undefined) updatePayload.category_name = data.category_name;
|
||||
|
||||
|
||||
if (data.category_code !== undefined) updatePayload.category_code = data.category_code;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await item_categories.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const item_categories = await db.item_categories.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of item_categories) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of item_categories) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const item_categories = await db.item_categories.findByPk(id, options);
|
||||
|
||||
await item_categories.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await item_categories.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const item_categories = await db.item_categories.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!item_categories) {
|
||||
return item_categories;
|
||||
}
|
||||
|
||||
const output = item_categories.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.items_category = await item_categories.getItems_category({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.category_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'item_categories',
|
||||
'category_name',
|
||||
filter.category_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.category_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'item_categories',
|
||||
'category_code',
|
||||
filter.category_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'item_categories',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.item_categories.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'item_categories',
|
||||
'category_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.item_categories.findAll({
|
||||
attributes: [ 'id', 'category_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['category_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.category_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
876
backend/src/db/api/items.js
Normal file
876
backend/src/db/api/items.js
Normal file
@ -0,0 +1,876 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class ItemsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const items = await db.items.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
item_name: data.item_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
item_code: data.item_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
item_type: data.item_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lot_tracked: data.lot_tracked
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
serial_tracked: data.serial_tracked
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
standard_cost: data.standard_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_purchase_price: data.default_purchase_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_sale_price: data.default_sale_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shelf_life_days: data.shelf_life_days
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reorder_point: data.reorder_point
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reorder_qty: data.reorder_qty
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await items.setCategory( data.category || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await items.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.items.getTableName(),
|
||||
belongsToColumn: 'spec_documents',
|
||||
belongsToId: items.id,
|
||||
},
|
||||
data.spec_documents,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const itemsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
item_name: item.item_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
item_code: item.item_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
item_type: item.item_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lot_tracked: item.lot_tracked
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
serial_tracked: item.serial_tracked
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
standard_cost: item.standard_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_purchase_price: item.default_purchase_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_sale_price: item.default_sale_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shelf_life_days: item.shelf_life_days
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reorder_point: item.reorder_point
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reorder_qty: item.reorder_qty
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const items = await db.items.bulkCreate(itemsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.items.getTableName(),
|
||||
belongsToColumn: 'spec_documents',
|
||||
belongsToId: items[i].id,
|
||||
},
|
||||
data[i].spec_documents,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const items = await db.items.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.item_name !== undefined) updatePayload.item_name = data.item_name;
|
||||
|
||||
|
||||
if (data.item_code !== undefined) updatePayload.item_code = data.item_code;
|
||||
|
||||
|
||||
if (data.item_type !== undefined) updatePayload.item_type = data.item_type;
|
||||
|
||||
|
||||
if (data.lot_tracked !== undefined) updatePayload.lot_tracked = data.lot_tracked;
|
||||
|
||||
|
||||
if (data.serial_tracked !== undefined) updatePayload.serial_tracked = data.serial_tracked;
|
||||
|
||||
|
||||
if (data.standard_cost !== undefined) updatePayload.standard_cost = data.standard_cost;
|
||||
|
||||
|
||||
if (data.default_purchase_price !== undefined) updatePayload.default_purchase_price = data.default_purchase_price;
|
||||
|
||||
|
||||
if (data.default_sale_price !== undefined) updatePayload.default_sale_price = data.default_sale_price;
|
||||
|
||||
|
||||
if (data.shelf_life_days !== undefined) updatePayload.shelf_life_days = data.shelf_life_days;
|
||||
|
||||
|
||||
if (data.reorder_point !== undefined) updatePayload.reorder_point = data.reorder_point;
|
||||
|
||||
|
||||
if (data.reorder_qty !== undefined) updatePayload.reorder_qty = data.reorder_qty;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await items.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.category !== undefined) {
|
||||
await items.setCategory(
|
||||
|
||||
data.category,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await items.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.items.getTableName(),
|
||||
belongsToColumn: 'spec_documents',
|
||||
belongsToId: items.id,
|
||||
},
|
||||
data.spec_documents,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const items = await db.items.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of items) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of items) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const items = await db.items.findByPk(id, options);
|
||||
|
||||
await items.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await items.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const items = await db.items.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!items) {
|
||||
return items;
|
||||
}
|
||||
|
||||
const output = items.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.lots_item = await items.getLots_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.inventory_transactions_item = await items.getInventory_transactions_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.boms_parent_item = await items.getBoms_parent_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.bom_lines_component_item = await items.getBom_lines_component_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_item = await items.getWork_orders_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue_lines_component_item = await items.getMaterial_issue_lines_component_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.receipt_lines_item = await items.getReceipt_lines_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.shipment_lines_item = await items.getShipment_lines_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.qa_inspection_plans_item = await items.getQa_inspection_plans_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.qa_inspections_item = await items.getQa_inspections_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.nonconformances_item = await items.getNonconformances_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.category = await items.getCategory({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await items.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.spec_documents = await items.getSpec_documents({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.item_categories,
|
||||
as: 'category',
|
||||
|
||||
where: filter.category ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.category.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
category_name: {
|
||||
[Op.or]: filter.category.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'spec_documents',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.item_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'items',
|
||||
'item_name',
|
||||
filter.item_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.item_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'items',
|
||||
'item_code',
|
||||
filter.item_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'items',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.standard_costRange) {
|
||||
const [start, end] = filter.standard_costRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
standard_cost: {
|
||||
...where.standard_cost,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
standard_cost: {
|
||||
...where.standard_cost,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.default_purchase_priceRange) {
|
||||
const [start, end] = filter.default_purchase_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_purchase_price: {
|
||||
...where.default_purchase_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_purchase_price: {
|
||||
...where.default_purchase_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.default_sale_priceRange) {
|
||||
const [start, end] = filter.default_sale_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_sale_price: {
|
||||
...where.default_sale_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_sale_price: {
|
||||
...where.default_sale_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.shelf_life_daysRange) {
|
||||
const [start, end] = filter.shelf_life_daysRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
shelf_life_days: {
|
||||
...where.shelf_life_days,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
shelf_life_days: {
|
||||
...where.shelf_life_days,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.reorder_pointRange) {
|
||||
const [start, end] = filter.reorder_pointRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reorder_point: {
|
||||
...where.reorder_point,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reorder_point: {
|
||||
...where.reorder_point,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.reorder_qtyRange) {
|
||||
const [start, end] = filter.reorder_qtyRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reorder_qty: {
|
||||
...where.reorder_qty,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reorder_qty: {
|
||||
...where.reorder_qty,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.item_type) {
|
||||
where = {
|
||||
...where,
|
||||
item_type: filter.item_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.lot_tracked) {
|
||||
where = {
|
||||
...where,
|
||||
lot_tracked: filter.lot_tracked,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.serial_tracked) {
|
||||
where = {
|
||||
...where,
|
||||
serial_tracked: filter.serial_tracked,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.items.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'items',
|
||||
'item_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.items.findAll({
|
||||
attributes: [ 'id', 'item_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['item_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.item_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
483
backend/src/db/api/locations.js
Normal file
483
backend/src/db/api/locations.js
Normal file
@ -0,0 +1,483 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class LocationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const locations = await db.locations.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
location_name: data.location_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_code: data.location_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_type: data.location_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await locations.setWarehouse( data.warehouse || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const locationsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
location_name: item.location_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_code: item.location_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_type: item.location_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const locations = await db.locations.bulkCreate(locationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const locations = await db.locations.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.location_name !== undefined) updatePayload.location_name = data.location_name;
|
||||
|
||||
|
||||
if (data.location_code !== undefined) updatePayload.location_code = data.location_code;
|
||||
|
||||
|
||||
if (data.location_type !== undefined) updatePayload.location_type = data.location_type;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await locations.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.warehouse !== undefined) {
|
||||
await locations.setWarehouse(
|
||||
|
||||
data.warehouse,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const locations = await db.locations.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of locations) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of locations) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const locations = await db.locations.findByPk(id, options);
|
||||
|
||||
await locations.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await locations.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const locations = await db.locations.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!locations) {
|
||||
return locations;
|
||||
}
|
||||
|
||||
const output = locations.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_from_location = await locations.getInventory_transactions_from_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
output.inventory_transactions_to_location = await locations.getInventory_transactions_to_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue_lines_from_location = await locations.getMaterial_issue_lines_from_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.receipts_to_location = await locations.getReceipts_to_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.shipments_from_location = await locations.getShipments_from_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.warehouse = await locations.getWarehouse({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.warehouses,
|
||||
as: 'warehouse',
|
||||
|
||||
where: filter.warehouse ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.warehouse.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
warehouse_name: {
|
||||
[Op.or]: filter.warehouse.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.location_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'locations',
|
||||
'location_name',
|
||||
filter.location_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.location_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'locations',
|
||||
'location_code',
|
||||
filter.location_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.location_type) {
|
||||
where = {
|
||||
...where,
|
||||
location_type: filter.location_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.locations.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'locations',
|
||||
'location_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.locations.findAll({
|
||||
attributes: [ 'id', 'location_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['location_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.location_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
721
backend/src/db/api/lots.js
Normal file
721
backend/src/db/api/lots.js
Normal file
@ -0,0 +1,721 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class LotsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const lots = await db.lots.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
lot_code: data.lot_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_lot_code: data.supplier_lot_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
received_at: data.received_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
manufactured_at: data.manufactured_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
expires_at: data.expires_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quality_status: data.quality_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
certificate_number: data.certificate_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await lots.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await lots.setSupplier( data.supplier || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.lots.getTableName(),
|
||||
belongsToColumn: 'certificates',
|
||||
belongsToId: lots.id,
|
||||
},
|
||||
data.certificates,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return lots;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const lotsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
lot_code: item.lot_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_lot_code: item.supplier_lot_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
received_at: item.received_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
manufactured_at: item.manufactured_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
expires_at: item.expires_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quality_status: item.quality_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
certificate_number: item.certificate_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const lots = await db.lots.bulkCreate(lotsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < lots.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.lots.getTableName(),
|
||||
belongsToColumn: 'certificates',
|
||||
belongsToId: lots[i].id,
|
||||
},
|
||||
data[i].certificates,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return lots;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const lots = await db.lots.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.lot_code !== undefined) updatePayload.lot_code = data.lot_code;
|
||||
|
||||
|
||||
if (data.supplier_lot_code !== undefined) updatePayload.supplier_lot_code = data.supplier_lot_code;
|
||||
|
||||
|
||||
if (data.received_at !== undefined) updatePayload.received_at = data.received_at;
|
||||
|
||||
|
||||
if (data.manufactured_at !== undefined) updatePayload.manufactured_at = data.manufactured_at;
|
||||
|
||||
|
||||
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
|
||||
|
||||
|
||||
if (data.quality_status !== undefined) updatePayload.quality_status = data.quality_status;
|
||||
|
||||
|
||||
if (data.certificate_number !== undefined) updatePayload.certificate_number = data.certificate_number;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await lots.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await lots.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.supplier !== undefined) {
|
||||
await lots.setSupplier(
|
||||
|
||||
data.supplier,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.lots.getTableName(),
|
||||
belongsToColumn: 'certificates',
|
||||
belongsToId: lots.id,
|
||||
},
|
||||
data.certificates,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return lots;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const lots = await db.lots.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of lots) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of lots) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return lots;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const lots = await db.lots.findByPk(id, options);
|
||||
|
||||
await lots.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await lots.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return lots;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const lots = await db.lots.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!lots) {
|
||||
return lots;
|
||||
}
|
||||
|
||||
const output = lots.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_lot = await lots.getInventory_transactions_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue_lines_lot = await lots.getMaterial_issue_lines_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.receipt_lines_lot = await lots.getReceipt_lines_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.shipment_lines_lot = await lots.getShipment_lines_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_inspections_lot = await lots.getQa_inspections_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.nonconformances_lot = await lots.getNonconformances_lot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.item = await lots.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.supplier = await lots.getSupplier({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.certificates = await lots.getCertificates({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.suppliers,
|
||||
as: 'supplier',
|
||||
|
||||
where: filter.supplier ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.supplier.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
supplier_name: {
|
||||
[Op.or]: filter.supplier.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'certificates',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.lot_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'lots',
|
||||
'lot_code',
|
||||
filter.lot_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.supplier_lot_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'lots',
|
||||
'supplier_lot_code',
|
||||
filter.supplier_lot_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.certificate_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'lots',
|
||||
'certificate_number',
|
||||
filter.certificate_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'lots',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
received_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
expires_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.received_atRange) {
|
||||
const [start, end] = filter.received_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
received_at: {
|
||||
...where.received_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
received_at: {
|
||||
...where.received_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.manufactured_atRange) {
|
||||
const [start, end] = filter.manufactured_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
manufactured_at: {
|
||||
...where.manufactured_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
manufactured_at: {
|
||||
...where.manufactured_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.expires_atRange) {
|
||||
const [start, end] = filter.expires_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
expires_at: {
|
||||
...where.expires_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
expires_at: {
|
||||
...where.expires_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.quality_status) {
|
||||
where = {
|
||||
...where,
|
||||
quality_status: filter.quality_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.lots.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'lots',
|
||||
'lot_code',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.lots.findAll({
|
||||
attributes: [ 'id', 'lot_code' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['lot_code', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.lot_code,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
645
backend/src/db/api/machine_downtime_events.js
Normal file
645
backend/src/db/api/machine_downtime_events.js
Normal file
@ -0,0 +1,645 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Machine_downtime_eventsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machine_downtime_events = await db.machine_downtime_events.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
started_at: data.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: data.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
downtime_category: data.downtime_category
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
severity: data.severity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reason: data.reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await machine_downtime_events.setMachine( data.machine || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await machine_downtime_events.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await machine_downtime_events.setReported_by( data.reported_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machine_downtime_events.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: machine_downtime_events.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const machine_downtime_eventsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
started_at: item.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: item.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
downtime_category: item.downtime_category
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
severity: item.severity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reason: item.reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const machine_downtime_events = await db.machine_downtime_events.bulkCreate(machine_downtime_eventsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < machine_downtime_events.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machine_downtime_events.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: machine_downtime_events[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const machine_downtime_events = await db.machine_downtime_events.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
||||
|
||||
|
||||
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
|
||||
|
||||
|
||||
if (data.downtime_category !== undefined) updatePayload.downtime_category = data.downtime_category;
|
||||
|
||||
|
||||
if (data.severity !== undefined) updatePayload.severity = data.severity;
|
||||
|
||||
|
||||
if (data.reason !== undefined) updatePayload.reason = data.reason;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await machine_downtime_events.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.machine !== undefined) {
|
||||
await machine_downtime_events.setMachine(
|
||||
|
||||
data.machine,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await machine_downtime_events.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.reported_by !== undefined) {
|
||||
await machine_downtime_events.setReported_by(
|
||||
|
||||
data.reported_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machine_downtime_events.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: machine_downtime_events.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machine_downtime_events = await db.machine_downtime_events.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of machine_downtime_events) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of machine_downtime_events) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machine_downtime_events = await db.machine_downtime_events.findByPk(id, options);
|
||||
|
||||
await machine_downtime_events.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await machine_downtime_events.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machine_downtime_events = await db.machine_downtime_events.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!machine_downtime_events) {
|
||||
return machine_downtime_events;
|
||||
}
|
||||
|
||||
const output = machine_downtime_events.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.machine = await machine_downtime_events.getMachine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.work_order = await machine_downtime_events.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.reported_by = await machine_downtime_events.getReported_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await machine_downtime_events.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.machines,
|
||||
as: 'machine',
|
||||
|
||||
where: filter.machine ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.machine.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
machine_name: {
|
||||
[Op.or]: filter.machine.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'reported_by',
|
||||
|
||||
where: filter.reported_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.reported_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.reported_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.reason) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machine_downtime_events',
|
||||
'reason',
|
||||
filter.reason,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
started_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
ended_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.started_atRange) {
|
||||
const [start, end] = filter.started_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
started_at: {
|
||||
...where.started_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
started_at: {
|
||||
...where.started_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.ended_atRange) {
|
||||
const [start, end] = filter.ended_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
ended_at: {
|
||||
...where.ended_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
ended_at: {
|
||||
...where.ended_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.downtime_category) {
|
||||
where = {
|
||||
...where,
|
||||
downtime_category: filter.downtime_category,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.severity) {
|
||||
where = {
|
||||
...where,
|
||||
severity: filter.severity,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.machine_downtime_events.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'machine_downtime_events',
|
||||
'reason',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.machine_downtime_events.findAll({
|
||||
attributes: [ 'id', 'reason' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['reason', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.reason,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
633
backend/src/db/api/machines.js
Normal file
633
backend/src/db/api/machines.js
Normal file
@ -0,0 +1,633 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class MachinesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machines = await db.machines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
machine_name: data.machine_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
machine_code: data.machine_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
machine_type: data.machine_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
manufacturer: data.manufacturer
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
model: data.model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
serial_number: data.serial_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
commissioned_at: data.commissioned_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_detail: data.location_detail
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machines.getTableName(),
|
||||
belongsToColumn: 'manuals',
|
||||
belongsToId: machines.id,
|
||||
},
|
||||
data.manuals,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return machines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const machinesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
machine_name: item.machine_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
machine_code: item.machine_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
machine_type: item.machine_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
manufacturer: item.manufacturer
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
model: item.model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
serial_number: item.serial_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
commissioned_at: item.commissioned_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
location_detail: item.location_detail
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const machines = await db.machines.bulkCreate(machinesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < machines.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machines.getTableName(),
|
||||
belongsToColumn: 'manuals',
|
||||
belongsToId: machines[i].id,
|
||||
},
|
||||
data[i].manuals,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return machines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const machines = await db.machines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.machine_name !== undefined) updatePayload.machine_name = data.machine_name;
|
||||
|
||||
|
||||
if (data.machine_code !== undefined) updatePayload.machine_code = data.machine_code;
|
||||
|
||||
|
||||
if (data.machine_type !== undefined) updatePayload.machine_type = data.machine_type;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.manufacturer !== undefined) updatePayload.manufacturer = data.manufacturer;
|
||||
|
||||
|
||||
if (data.model !== undefined) updatePayload.model = data.model;
|
||||
|
||||
|
||||
if (data.serial_number !== undefined) updatePayload.serial_number = data.serial_number;
|
||||
|
||||
|
||||
if (data.commissioned_at !== undefined) updatePayload.commissioned_at = data.commissioned_at;
|
||||
|
||||
|
||||
if (data.location_detail !== undefined) updatePayload.location_detail = data.location_detail;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await machines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.machines.getTableName(),
|
||||
belongsToColumn: 'manuals',
|
||||
belongsToId: machines.id,
|
||||
},
|
||||
data.manuals,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return machines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machines = await db.machines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of machines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of machines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return machines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machines = await db.machines.findByPk(id, options);
|
||||
|
||||
await machines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await machines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return machines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const machines = await db.machines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!machines) {
|
||||
return machines;
|
||||
}
|
||||
|
||||
const output = machines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.machine_downtime_events_machine = await machines.getMachine_downtime_events_machine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_primary_machine = await machines.getWork_orders_primary_machine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.work_order_operations_machine = await machines.getWork_order_operations_machine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.manuals = await machines.getManuals({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'manuals',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.machine_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'machine_name',
|
||||
filter.machine_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.machine_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'machine_code',
|
||||
filter.machine_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.manufacturer) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'manufacturer',
|
||||
filter.manufacturer,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.model) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'model',
|
||||
filter.model,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.serial_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'serial_number',
|
||||
filter.serial_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.location_detail) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'machines',
|
||||
'location_detail',
|
||||
filter.location_detail,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.commissioned_atRange) {
|
||||
const [start, end] = filter.commissioned_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
commissioned_at: {
|
||||
...where.commissioned_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
commissioned_at: {
|
||||
...where.commissioned_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.machine_type) {
|
||||
where = {
|
||||
...where,
|
||||
machine_type: filter.machine_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.machines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'machines',
|
||||
'machine_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.machines.findAll({
|
||||
attributes: [ 'id', 'machine_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['machine_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.machine_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
602
backend/src/db/api/material_issue_lines.js
Normal file
602
backend/src/db/api/material_issue_lines.js
Normal file
@ -0,0 +1,602 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Material_issue_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issue_lines = await db.material_issue_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
quantity: data.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_status: data.line_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: data.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await material_issue_lines.setMaterial_issue( data.material_issue || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issue_lines.setComponent_item( data.component_item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issue_lines.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issue_lines.setFrom_location( data.from_location || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issue_lines.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const material_issue_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
quantity: item.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_status: item.line_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: item.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const material_issue_lines = await db.material_issue_lines.bulkCreate(material_issue_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const material_issue_lines = await db.material_issue_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
|
||||
|
||||
|
||||
if (data.line_status !== undefined) updatePayload.line_status = data.line_status;
|
||||
|
||||
|
||||
if (data.line_notes !== undefined) updatePayload.line_notes = data.line_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await material_issue_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.material_issue !== undefined) {
|
||||
await material_issue_lines.setMaterial_issue(
|
||||
|
||||
data.material_issue,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.component_item !== undefined) {
|
||||
await material_issue_lines.setComponent_item(
|
||||
|
||||
data.component_item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await material_issue_lines.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.from_location !== undefined) {
|
||||
await material_issue_lines.setFrom_location(
|
||||
|
||||
data.from_location,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await material_issue_lines.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issue_lines = await db.material_issue_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of material_issue_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of material_issue_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issue_lines = await db.material_issue_lines.findByPk(id, options);
|
||||
|
||||
await material_issue_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issue_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issue_lines = await db.material_issue_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!material_issue_lines) {
|
||||
return material_issue_lines;
|
||||
}
|
||||
|
||||
const output = material_issue_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue = await material_issue_lines.getMaterial_issue({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.component_item = await material_issue_lines.getComponent_item({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await material_issue_lines.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.from_location = await material_issue_lines.getFrom_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await material_issue_lines.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.material_issues,
|
||||
as: 'material_issue',
|
||||
|
||||
where: filter.material_issue ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.material_issue.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
issue_number: {
|
||||
[Op.or]: filter.material_issue.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'component_item',
|
||||
|
||||
where: filter.component_item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.component_item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.component_item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.locations,
|
||||
as: 'from_location',
|
||||
|
||||
where: filter.from_location ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.from_location.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
location_name: {
|
||||
[Op.or]: filter.from_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'material_issue_lines',
|
||||
'line_notes',
|
||||
filter.line_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.quantityRange) {
|
||||
const [start, end] = filter.quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_status) {
|
||||
where = {
|
||||
...where,
|
||||
line_status: filter.line_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.material_issue_lines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'material_issue_lines',
|
||||
'line_notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.material_issue_lines.findAll({
|
||||
attributes: [ 'id', 'line_notes' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['line_notes', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.line_notes,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
561
backend/src/db/api/material_issues.js
Normal file
561
backend/src/db/api/material_issues.js
Normal file
@ -0,0 +1,561 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Material_issuesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issues = await db.material_issues.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
issue_number: data.issue_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issued_at: data.issued_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_type: data.issue_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await material_issues.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issues.setIssued_by( data.issued_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.material_issues.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: material_issues.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const material_issuesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
issue_number: item.issue_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issued_at: item.issued_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_type: item.issue_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const material_issues = await db.material_issues.bulkCreate(material_issuesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < material_issues.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.material_issues.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: material_issues[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const material_issues = await db.material_issues.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.issue_number !== undefined) updatePayload.issue_number = data.issue_number;
|
||||
|
||||
|
||||
if (data.issued_at !== undefined) updatePayload.issued_at = data.issued_at;
|
||||
|
||||
|
||||
if (data.issue_type !== undefined) updatePayload.issue_type = data.issue_type;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await material_issues.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await material_issues.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.issued_by !== undefined) {
|
||||
await material_issues.setIssued_by(
|
||||
|
||||
data.issued_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.material_issues.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: material_issues.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issues = await db.material_issues.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of material_issues) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of material_issues) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issues = await db.material_issues.findByPk(id, options);
|
||||
|
||||
await material_issues.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await material_issues.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const material_issues = await db.material_issues.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!material_issues) {
|
||||
return material_issues;
|
||||
}
|
||||
|
||||
const output = material_issues.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue_lines_material_issue = await material_issues.getMaterial_issue_lines_material_issue({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_order = await material_issues.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.issued_by = await material_issues.getIssued_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await material_issues.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'issued_by',
|
||||
|
||||
where: filter.issued_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.issued_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.issued_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.issue_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'material_issues',
|
||||
'issue_number',
|
||||
filter.issue_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'material_issues',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.issued_atRange) {
|
||||
const [start, end] = filter.issued_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
issued_at: {
|
||||
...where.issued_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
issued_at: {
|
||||
...where.issued_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.issue_type) {
|
||||
where = {
|
||||
...where,
|
||||
issue_type: filter.issue_type,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.material_issues.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'material_issues',
|
||||
'issue_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.material_issues.findAll({
|
||||
attributes: [ 'id', 'issue_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['issue_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.issue_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
911
backend/src/db/api/nonconformances.js
Normal file
911
backend/src/db/api/nonconformances.js
Normal file
@ -0,0 +1,911 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class NonconformancesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const nonconformances = await db.nonconformances.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
nc_number: data.nc_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
severity: data.severity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
source: data.source
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reported_at: data.reported_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
problem_description: data.problem_description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disposition: data.disposition
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
containment_action: data.containment_action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
root_cause: data.root_cause
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
corrective_action: data.corrective_action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: data.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
closed_at: data.closed_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await nonconformances.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.setQa_inspection( data.qa_inspection || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.setReported_by( data.reported_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.setOwner( data.owner || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.nonconformances.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: nonconformances.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const nonconformancesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
nc_number: item.nc_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
severity: item.severity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
source: item.source
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reported_at: item.reported_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
problem_description: item.problem_description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disposition: item.disposition
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
containment_action: item.containment_action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
root_cause: item.root_cause
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
corrective_action: item.corrective_action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: item.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
closed_at: item.closed_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const nonconformances = await db.nonconformances.bulkCreate(nonconformancesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < nonconformances.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.nonconformances.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: nonconformances[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const nonconformances = await db.nonconformances.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.nc_number !== undefined) updatePayload.nc_number = data.nc_number;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.severity !== undefined) updatePayload.severity = data.severity;
|
||||
|
||||
|
||||
if (data.source !== undefined) updatePayload.source = data.source;
|
||||
|
||||
|
||||
if (data.reported_at !== undefined) updatePayload.reported_at = data.reported_at;
|
||||
|
||||
|
||||
if (data.problem_description !== undefined) updatePayload.problem_description = data.problem_description;
|
||||
|
||||
|
||||
if (data.disposition !== undefined) updatePayload.disposition = data.disposition;
|
||||
|
||||
|
||||
if (data.containment_action !== undefined) updatePayload.containment_action = data.containment_action;
|
||||
|
||||
|
||||
if (data.root_cause !== undefined) updatePayload.root_cause = data.root_cause;
|
||||
|
||||
|
||||
if (data.corrective_action !== undefined) updatePayload.corrective_action = data.corrective_action;
|
||||
|
||||
|
||||
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
||||
|
||||
|
||||
if (data.closed_at !== undefined) updatePayload.closed_at = data.closed_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await nonconformances.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await nonconformances.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await nonconformances.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await nonconformances.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.qa_inspection !== undefined) {
|
||||
await nonconformances.setQa_inspection(
|
||||
|
||||
data.qa_inspection,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.reported_by !== undefined) {
|
||||
await nonconformances.setReported_by(
|
||||
|
||||
data.reported_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.owner !== undefined) {
|
||||
await nonconformances.setOwner(
|
||||
|
||||
data.owner,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.nonconformances.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: nonconformances.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const nonconformances = await db.nonconformances.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of nonconformances) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of nonconformances) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const nonconformances = await db.nonconformances.findByPk(id, options);
|
||||
|
||||
await nonconformances.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await nonconformances.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const nonconformances = await db.nonconformances.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!nonconformances) {
|
||||
return nonconformances;
|
||||
}
|
||||
|
||||
const output = nonconformances.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.item = await nonconformances.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await nonconformances.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.work_order = await nonconformances.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.qa_inspection = await nonconformances.getQa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.reported_by = await nonconformances.getReported_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.owner = await nonconformances.getOwner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await nonconformances.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.qa_inspections,
|
||||
as: 'qa_inspection',
|
||||
|
||||
where: filter.qa_inspection ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.qa_inspection.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
inspection_number: {
|
||||
[Op.or]: filter.qa_inspection.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'reported_by',
|
||||
|
||||
where: filter.reported_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.reported_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.reported_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'owner',
|
||||
|
||||
where: filter.owner ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.owner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.nc_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'nonconformances',
|
||||
'nc_number',
|
||||
filter.nc_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.problem_description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'nonconformances',
|
||||
'problem_description',
|
||||
filter.problem_description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.containment_action) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'nonconformances',
|
||||
'containment_action',
|
||||
filter.containment_action,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.root_cause) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'nonconformances',
|
||||
'root_cause',
|
||||
filter.root_cause,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.corrective_action) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'nonconformances',
|
||||
'corrective_action',
|
||||
filter.corrective_action,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.reported_atRange) {
|
||||
const [start, end] = filter.reported_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reported_at: {
|
||||
...where.reported_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reported_at: {
|
||||
...where.reported_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.due_atRange) {
|
||||
const [start, end] = filter.due_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
due_at: {
|
||||
...where.due_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
due_at: {
|
||||
...where.due_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.closed_atRange) {
|
||||
const [start, end] = filter.closed_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
closed_at: {
|
||||
...where.closed_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
closed_at: {
|
||||
...where.closed_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.severity) {
|
||||
where = {
|
||||
...where,
|
||||
severity: filter.severity,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.source) {
|
||||
where = {
|
||||
...where,
|
||||
source: filter.source,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.disposition) {
|
||||
where = {
|
||||
...where,
|
||||
disposition: filter.disposition,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.nonconformances.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'nonconformances',
|
||||
'nc_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.nonconformances.findAll({
|
||||
attributes: [ 'id', 'nc_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['nc_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.nc_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
643
backend/src/db/api/operations.js
Normal file
643
backend/src/db/api/operations.js
Normal file
@ -0,0 +1,643 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class OperationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const operations = await db.operations.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
operation_name: data.operation_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
operation_code: data.operation_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sequence: data.sequence
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
setup_time_minutes: data.setup_time_minutes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
run_time_minutes_per_unit: data.run_time_minutes_per_unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
labor_time_minutes_per_unit: data.labor_time_minutes_per_unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
work_instructions: data.work_instructions
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await operations.setRoute( data.route || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.operations.getTableName(),
|
||||
belongsToColumn: 'instruction_attachments',
|
||||
belongsToId: operations.id,
|
||||
},
|
||||
data.instruction_attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const operationsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
operation_name: item.operation_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
operation_code: item.operation_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sequence: item.sequence
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
setup_time_minutes: item.setup_time_minutes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
run_time_minutes_per_unit: item.run_time_minutes_per_unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
labor_time_minutes_per_unit: item.labor_time_minutes_per_unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
work_instructions: item.work_instructions
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const operations = await db.operations.bulkCreate(operationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < operations.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.operations.getTableName(),
|
||||
belongsToColumn: 'instruction_attachments',
|
||||
belongsToId: operations[i].id,
|
||||
},
|
||||
data[i].instruction_attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const operations = await db.operations.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.operation_name !== undefined) updatePayload.operation_name = data.operation_name;
|
||||
|
||||
|
||||
if (data.operation_code !== undefined) updatePayload.operation_code = data.operation_code;
|
||||
|
||||
|
||||
if (data.sequence !== undefined) updatePayload.sequence = data.sequence;
|
||||
|
||||
|
||||
if (data.setup_time_minutes !== undefined) updatePayload.setup_time_minutes = data.setup_time_minutes;
|
||||
|
||||
|
||||
if (data.run_time_minutes_per_unit !== undefined) updatePayload.run_time_minutes_per_unit = data.run_time_minutes_per_unit;
|
||||
|
||||
|
||||
if (data.labor_time_minutes_per_unit !== undefined) updatePayload.labor_time_minutes_per_unit = data.labor_time_minutes_per_unit;
|
||||
|
||||
|
||||
if (data.work_instructions !== undefined) updatePayload.work_instructions = data.work_instructions;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await operations.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.route !== undefined) {
|
||||
await operations.setRoute(
|
||||
|
||||
data.route,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.operations.getTableName(),
|
||||
belongsToColumn: 'instruction_attachments',
|
||||
belongsToId: operations.id,
|
||||
},
|
||||
data.instruction_attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const operations = await db.operations.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of operations) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of operations) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const operations = await db.operations.findByPk(id, options);
|
||||
|
||||
await operations.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await operations.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const operations = await db.operations.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!operations) {
|
||||
return operations;
|
||||
}
|
||||
|
||||
const output = operations.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_order_operations_operation = await operations.getWork_order_operations_operation({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_inspections_operation = await operations.getQa_inspections_operation({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.route = await operations.getRoute({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.instruction_attachments = await operations.getInstruction_attachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.routes,
|
||||
as: 'route',
|
||||
|
||||
where: filter.route ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.route.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
route_name: {
|
||||
[Op.or]: filter.route.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'instruction_attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.operation_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'operations',
|
||||
'operation_name',
|
||||
filter.operation_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.operation_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'operations',
|
||||
'operation_code',
|
||||
filter.operation_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.work_instructions) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'operations',
|
||||
'work_instructions',
|
||||
filter.work_instructions,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.sequenceRange) {
|
||||
const [start, end] = filter.sequenceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sequence: {
|
||||
...where.sequence,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sequence: {
|
||||
...where.sequence,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.setup_time_minutesRange) {
|
||||
const [start, end] = filter.setup_time_minutesRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
setup_time_minutes: {
|
||||
...where.setup_time_minutes,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
setup_time_minutes: {
|
||||
...where.setup_time_minutes,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.run_time_minutes_per_unitRange) {
|
||||
const [start, end] = filter.run_time_minutes_per_unitRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
run_time_minutes_per_unit: {
|
||||
...where.run_time_minutes_per_unit,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
run_time_minutes_per_unit: {
|
||||
...where.run_time_minutes_per_unit,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.labor_time_minutes_per_unitRange) {
|
||||
const [start, end] = filter.labor_time_minutes_per_unitRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
labor_time_minutes_per_unit: {
|
||||
...where.labor_time_minutes_per_unit,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
labor_time_minutes_per_unit: {
|
||||
...where.labor_time_minutes_per_unit,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.operations.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'operations',
|
||||
'operation_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.operations.findAll({
|
||||
attributes: [ 'id', 'operation_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['operation_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.operation_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
360
backend/src/db/api/permissions.js
Normal file
360
backend/src/db/api/permissions.js
Normal file
@ -0,0 +1,360 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class PermissionsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const permissions = await db.permissions.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const permissionsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const permissions = await db.permissions.bulkCreate(permissionsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const permissions = await db.permissions.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await permissions.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const permissions = await db.permissions.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of permissions) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of permissions) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const permissions = await db.permissions.findByPk(id, options);
|
||||
|
||||
await permissions.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await permissions.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const permissions = await db.permissions.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!permissions) {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
const output = permissions.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'permissions',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.permissions.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'permissions',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.permissions.findAll({
|
||||
attributes: [ 'id', 'name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
469
backend/src/db/api/production_lines.js
Normal file
469
backend/src/db/api/production_lines.js
Normal file
@ -0,0 +1,469 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Production_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const production_lines = await db.production_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
line_name: data.line_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_code: data.line_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_status: data.line_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await production_lines.setWork_center( data.work_center || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const production_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
line_name: item.line_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_code: item.line_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_status: item.line_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const production_lines = await db.production_lines.bulkCreate(production_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const production_lines = await db.production_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.line_name !== undefined) updatePayload.line_name = data.line_name;
|
||||
|
||||
|
||||
if (data.line_code !== undefined) updatePayload.line_code = data.line_code;
|
||||
|
||||
|
||||
if (data.line_status !== undefined) updatePayload.line_status = data.line_status;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await production_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.work_center !== undefined) {
|
||||
await production_lines.setWork_center(
|
||||
|
||||
data.work_center,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const production_lines = await db.production_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of production_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of production_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const production_lines = await db.production_lines.findByPk(id, options);
|
||||
|
||||
await production_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await production_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const production_lines = await db.production_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!production_lines) {
|
||||
return production_lines;
|
||||
}
|
||||
|
||||
const output = production_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_production_line = await production_lines.getWork_orders_production_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_center = await production_lines.getWork_center({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.work_centers,
|
||||
as: 'work_center',
|
||||
|
||||
where: filter.work_center ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_center.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_center_name: {
|
||||
[Op.or]: filter.work_center.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'production_lines',
|
||||
'line_name',
|
||||
filter.line_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.line_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'production_lines',
|
||||
'line_code',
|
||||
filter.line_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'production_lines',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_status) {
|
||||
where = {
|
||||
...where,
|
||||
line_status: filter.line_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.production_lines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'production_lines',
|
||||
'line_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.production_lines.findAll({
|
||||
attributes: [ 'id', 'line_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['line_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.line_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
620
backend/src/db/api/qa_characteristics.js
Normal file
620
backend/src/db/api/qa_characteristics.js
Normal file
@ -0,0 +1,620 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Qa_characteristicsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_characteristics = await db.qa_characteristics.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
characteristic_name: data.characteristic_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
characteristic_code: data.characteristic_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
data_type: data.data_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
target_value: data.target_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lower_limit: data.lower_limit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
upper_limit: data.upper_limit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_pass_value: data.attribute_pass_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_fail_value: data.attribute_fail_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
test_method: data.test_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await qa_characteristics.setInspection_plan( data.inspection_plan || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const qa_characteristicsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
characteristic_name: item.characteristic_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
characteristic_code: item.characteristic_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
data_type: item.data_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
target_value: item.target_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lower_limit: item.lower_limit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
upper_limit: item.upper_limit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_pass_value: item.attribute_pass_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_fail_value: item.attribute_fail_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
test_method: item.test_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const qa_characteristics = await db.qa_characteristics.bulkCreate(qa_characteristicsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const qa_characteristics = await db.qa_characteristics.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.characteristic_name !== undefined) updatePayload.characteristic_name = data.characteristic_name;
|
||||
|
||||
|
||||
if (data.characteristic_code !== undefined) updatePayload.characteristic_code = data.characteristic_code;
|
||||
|
||||
|
||||
if (data.data_type !== undefined) updatePayload.data_type = data.data_type;
|
||||
|
||||
|
||||
if (data.target_value !== undefined) updatePayload.target_value = data.target_value;
|
||||
|
||||
|
||||
if (data.lower_limit !== undefined) updatePayload.lower_limit = data.lower_limit;
|
||||
|
||||
|
||||
if (data.upper_limit !== undefined) updatePayload.upper_limit = data.upper_limit;
|
||||
|
||||
|
||||
if (data.attribute_pass_value !== undefined) updatePayload.attribute_pass_value = data.attribute_pass_value;
|
||||
|
||||
|
||||
if (data.attribute_fail_value !== undefined) updatePayload.attribute_fail_value = data.attribute_fail_value;
|
||||
|
||||
|
||||
if (data.test_method !== undefined) updatePayload.test_method = data.test_method;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await qa_characteristics.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.inspection_plan !== undefined) {
|
||||
await qa_characteristics.setInspection_plan(
|
||||
|
||||
data.inspection_plan,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_characteristics = await db.qa_characteristics.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of qa_characteristics) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of qa_characteristics) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_characteristics = await db.qa_characteristics.findByPk(id, options);
|
||||
|
||||
await qa_characteristics.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_characteristics.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_characteristics = await db.qa_characteristics.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!qa_characteristics) {
|
||||
return qa_characteristics;
|
||||
}
|
||||
|
||||
const output = qa_characteristics.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_results_characteristic = await qa_characteristics.getQa_results_characteristic({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inspection_plan = await qa_characteristics.getInspection_plan({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.qa_inspection_plans,
|
||||
as: 'inspection_plan',
|
||||
|
||||
where: filter.inspection_plan ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.inspection_plan.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
plan_name: {
|
||||
[Op.or]: filter.inspection_plan.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.characteristic_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_characteristics',
|
||||
'characteristic_name',
|
||||
filter.characteristic_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.characteristic_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_characteristics',
|
||||
'characteristic_code',
|
||||
filter.characteristic_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.test_method) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_characteristics',
|
||||
'test_method',
|
||||
filter.test_method,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.target_valueRange) {
|
||||
const [start, end] = filter.target_valueRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
target_value: {
|
||||
...where.target_value,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
target_value: {
|
||||
...where.target_value,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.lower_limitRange) {
|
||||
const [start, end] = filter.lower_limitRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
lower_limit: {
|
||||
...where.lower_limit,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
lower_limit: {
|
||||
...where.lower_limit,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.upper_limitRange) {
|
||||
const [start, end] = filter.upper_limitRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
upper_limit: {
|
||||
...where.upper_limit,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
upper_limit: {
|
||||
...where.upper_limit,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.data_type) {
|
||||
where = {
|
||||
...where,
|
||||
data_type: filter.data_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.attribute_pass_value) {
|
||||
where = {
|
||||
...where,
|
||||
attribute_pass_value: filter.attribute_pass_value,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.attribute_fail_value) {
|
||||
where = {
|
||||
...where,
|
||||
attribute_fail_value: filter.attribute_fail_value,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.qa_characteristics.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'qa_characteristics',
|
||||
'characteristic_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.qa_characteristics.findAll({
|
||||
attributes: [ 'id', 'characteristic_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['characteristic_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.characteristic_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
592
backend/src/db/api/qa_inspection_plans.js
Normal file
592
backend/src/db/api/qa_inspection_plans.js
Normal file
@ -0,0 +1,592 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Qa_inspection_plansDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
plan_name: data.plan_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
plan_code: data.plan_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
inspection_type: data.inspection_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sampling_method: data.sampling_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sample_size: data.sample_size
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
plan_status: data.plan_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await qa_inspection_plans.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspection_plans.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspection_plans.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const qa_inspection_plansData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
plan_name: item.plan_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
plan_code: item.plan_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
inspection_type: item.inspection_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sampling_method: item.sampling_method
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sample_size: item.sample_size
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
plan_status: item.plan_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.bulkCreate(qa_inspection_plansData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < qa_inspection_plans.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspection_plans.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspection_plans[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.plan_name !== undefined) updatePayload.plan_name = data.plan_name;
|
||||
|
||||
|
||||
if (data.plan_code !== undefined) updatePayload.plan_code = data.plan_code;
|
||||
|
||||
|
||||
if (data.inspection_type !== undefined) updatePayload.inspection_type = data.inspection_type;
|
||||
|
||||
|
||||
if (data.sampling_method !== undefined) updatePayload.sampling_method = data.sampling_method;
|
||||
|
||||
|
||||
if (data.sample_size !== undefined) updatePayload.sample_size = data.sample_size;
|
||||
|
||||
|
||||
if (data.plan_status !== undefined) updatePayload.plan_status = data.plan_status;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await qa_inspection_plans.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await qa_inspection_plans.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspection_plans.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspection_plans.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of qa_inspection_plans) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of qa_inspection_plans) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.findByPk(id, options);
|
||||
|
||||
await qa_inspection_plans.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspection_plans.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspection_plans = await db.qa_inspection_plans.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!qa_inspection_plans) {
|
||||
return qa_inspection_plans;
|
||||
}
|
||||
|
||||
const output = qa_inspection_plans.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_characteristics_inspection_plan = await qa_inspection_plans.getQa_characteristics_inspection_plan({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.qa_inspections_inspection_plan = await qa_inspection_plans.getQa_inspections_inspection_plan({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.item = await qa_inspection_plans.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await qa_inspection_plans.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.plan_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_inspection_plans',
|
||||
'plan_name',
|
||||
filter.plan_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.plan_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_inspection_plans',
|
||||
'plan_code',
|
||||
filter.plan_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_inspection_plans',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.sample_sizeRange) {
|
||||
const [start, end] = filter.sample_sizeRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sample_size: {
|
||||
...where.sample_size,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sample_size: {
|
||||
...where.sample_size,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.inspection_type) {
|
||||
where = {
|
||||
...where,
|
||||
inspection_type: filter.inspection_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.sampling_method) {
|
||||
where = {
|
||||
...where,
|
||||
sampling_method: filter.sampling_method,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.plan_status) {
|
||||
where = {
|
||||
...where,
|
||||
plan_status: filter.plan_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.qa_inspection_plans.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'qa_inspection_plans',
|
||||
'plan_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.qa_inspection_plans.findAll({
|
||||
attributes: [ 'id', 'plan_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['plan_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.plan_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
868
backend/src/db/api/qa_inspections.js
Normal file
868
backend/src/db/api/qa_inspections.js
Normal file
@ -0,0 +1,868 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Qa_inspectionsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspections = await db.qa_inspections.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
inspection_number: data.inspection_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
inspection_type: data.inspection_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_at: data.scheduled_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
performed_at: data.performed_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sample_size: data.sample_size
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
defect_count: data.defect_count
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disposition: data.disposition
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
summary: data.summary
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await qa_inspections.setInspection_plan( data.inspection_plan || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.setOperation( data.operation || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.setInspector( data.inspector || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspections.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspections.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const qa_inspectionsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
inspection_number: item.inspection_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
inspection_type: item.inspection_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_at: item.scheduled_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
performed_at: item.performed_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sample_size: item.sample_size
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
defect_count: item.defect_count
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disposition: item.disposition
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
summary: item.summary
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const qa_inspections = await db.qa_inspections.bulkCreate(qa_inspectionsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < qa_inspections.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspections.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspections[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const qa_inspections = await db.qa_inspections.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.inspection_number !== undefined) updatePayload.inspection_number = data.inspection_number;
|
||||
|
||||
|
||||
if (data.inspection_type !== undefined) updatePayload.inspection_type = data.inspection_type;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.scheduled_at !== undefined) updatePayload.scheduled_at = data.scheduled_at;
|
||||
|
||||
|
||||
if (data.performed_at !== undefined) updatePayload.performed_at = data.performed_at;
|
||||
|
||||
|
||||
if (data.sample_size !== undefined) updatePayload.sample_size = data.sample_size;
|
||||
|
||||
|
||||
if (data.defect_count !== undefined) updatePayload.defect_count = data.defect_count;
|
||||
|
||||
|
||||
if (data.disposition !== undefined) updatePayload.disposition = data.disposition;
|
||||
|
||||
|
||||
if (data.summary !== undefined) updatePayload.summary = data.summary;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await qa_inspections.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.inspection_plan !== undefined) {
|
||||
await qa_inspections.setInspection_plan(
|
||||
|
||||
data.inspection_plan,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await qa_inspections.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await qa_inspections.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await qa_inspections.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.operation !== undefined) {
|
||||
await qa_inspections.setOperation(
|
||||
|
||||
data.operation,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.inspector !== undefined) {
|
||||
await qa_inspections.setInspector(
|
||||
|
||||
data.inspector,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.qa_inspections.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: qa_inspections.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspections = await db.qa_inspections.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of qa_inspections) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of qa_inspections) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspections = await db.qa_inspections.findByPk(id, options);
|
||||
|
||||
await qa_inspections.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_inspections.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_inspections = await db.qa_inspections.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!qa_inspections) {
|
||||
return qa_inspections;
|
||||
}
|
||||
|
||||
const output = qa_inspections.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_qa_inspection = await qa_inspections.getInventory_transactions_qa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_results_qa_inspection = await qa_inspections.getQa_results_qa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.nonconformances_qa_inspection = await qa_inspections.getNonconformances_qa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.inspection_plan = await qa_inspections.getInspection_plan({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.item = await qa_inspections.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await qa_inspections.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.work_order = await qa_inspections.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.operation = await qa_inspections.getOperation({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.inspector = await qa_inspections.getInspector({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await qa_inspections.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.qa_inspection_plans,
|
||||
as: 'inspection_plan',
|
||||
|
||||
where: filter.inspection_plan ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.inspection_plan.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
plan_name: {
|
||||
[Op.or]: filter.inspection_plan.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.operations,
|
||||
as: 'operation',
|
||||
|
||||
where: filter.operation ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.operation.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
operation_name: {
|
||||
[Op.or]: filter.operation.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'inspector',
|
||||
|
||||
where: filter.inspector ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.inspector.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.inspector.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.inspection_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_inspections',
|
||||
'inspection_number',
|
||||
filter.inspection_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.summary) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_inspections',
|
||||
'summary',
|
||||
filter.summary,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.scheduled_atRange) {
|
||||
const [start, end] = filter.scheduled_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_at: {
|
||||
...where.scheduled_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_at: {
|
||||
...where.scheduled_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.performed_atRange) {
|
||||
const [start, end] = filter.performed_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
performed_at: {
|
||||
...where.performed_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
performed_at: {
|
||||
...where.performed_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.sample_sizeRange) {
|
||||
const [start, end] = filter.sample_sizeRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sample_size: {
|
||||
...where.sample_size,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sample_size: {
|
||||
...where.sample_size,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.defect_countRange) {
|
||||
const [start, end] = filter.defect_countRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
defect_count: {
|
||||
...where.defect_count,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
defect_count: {
|
||||
...where.defect_count,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.inspection_type) {
|
||||
where = {
|
||||
...where,
|
||||
inspection_type: filter.inspection_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.disposition) {
|
||||
where = {
|
||||
...where,
|
||||
disposition: filter.disposition,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.qa_inspections.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'qa_inspections',
|
||||
'inspection_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.qa_inspections.findAll({
|
||||
attributes: [ 'id', 'inspection_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['inspection_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.inspection_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
513
backend/src/db/api/qa_results.js
Normal file
513
backend/src/db/api/qa_results.js
Normal file
@ -0,0 +1,513 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Qa_resultsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_results = await db.qa_results.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
measured_value: data.measured_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_result: data.attribute_result
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
within_limits: data.within_limits
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await qa_results.setQa_inspection( data.qa_inspection || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_results.setCharacteristic( data.characteristic || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const qa_resultsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
measured_value: item.measured_value
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
attribute_result: item.attribute_result
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
within_limits: item.within_limits
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const qa_results = await db.qa_results.bulkCreate(qa_resultsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const qa_results = await db.qa_results.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.measured_value !== undefined) updatePayload.measured_value = data.measured_value;
|
||||
|
||||
|
||||
if (data.attribute_result !== undefined) updatePayload.attribute_result = data.attribute_result;
|
||||
|
||||
|
||||
if (data.within_limits !== undefined) updatePayload.within_limits = data.within_limits;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await qa_results.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.qa_inspection !== undefined) {
|
||||
await qa_results.setQa_inspection(
|
||||
|
||||
data.qa_inspection,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.characteristic !== undefined) {
|
||||
await qa_results.setCharacteristic(
|
||||
|
||||
data.characteristic,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_results = await db.qa_results.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of qa_results) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of qa_results) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_results = await db.qa_results.findByPk(id, options);
|
||||
|
||||
await qa_results.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await qa_results.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const qa_results = await db.qa_results.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!qa_results) {
|
||||
return qa_results;
|
||||
}
|
||||
|
||||
const output = qa_results.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_inspection = await qa_results.getQa_inspection({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.characteristic = await qa_results.getCharacteristic({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.qa_inspections,
|
||||
as: 'qa_inspection',
|
||||
|
||||
where: filter.qa_inspection ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.qa_inspection.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
inspection_number: {
|
||||
[Op.or]: filter.qa_inspection.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.qa_characteristics,
|
||||
as: 'characteristic',
|
||||
|
||||
where: filter.characteristic ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.characteristic.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
characteristic_name: {
|
||||
[Op.or]: filter.characteristic.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'qa_results',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.measured_valueRange) {
|
||||
const [start, end] = filter.measured_valueRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
measured_value: {
|
||||
...where.measured_value,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
measured_value: {
|
||||
...where.measured_value,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.attribute_result) {
|
||||
where = {
|
||||
...where,
|
||||
attribute_result: filter.attribute_result,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.within_limits) {
|
||||
where = {
|
||||
...where,
|
||||
within_limits: filter.within_limits,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.qa_results.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'qa_results',
|
||||
'notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.qa_results.findAll({
|
||||
attributes: [ 'id', 'notes' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['notes', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.notes,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
602
backend/src/db/api/receipt_lines.js
Normal file
602
backend/src/db/api/receipt_lines.js
Normal file
@ -0,0 +1,602 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Receipt_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipt_lines = await db.receipt_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
quantity: data.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_cost: data.unit_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quality_status: data.quality_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: data.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await receipt_lines.setReceipt( data.receipt || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipt_lines.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipt_lines.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipt_lines.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const receipt_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
quantity: item.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_cost: item.unit_cost
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quality_status: item.quality_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: item.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const receipt_lines = await db.receipt_lines.bulkCreate(receipt_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const receipt_lines = await db.receipt_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
|
||||
|
||||
|
||||
if (data.unit_cost !== undefined) updatePayload.unit_cost = data.unit_cost;
|
||||
|
||||
|
||||
if (data.quality_status !== undefined) updatePayload.quality_status = data.quality_status;
|
||||
|
||||
|
||||
if (data.line_notes !== undefined) updatePayload.line_notes = data.line_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await receipt_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.receipt !== undefined) {
|
||||
await receipt_lines.setReceipt(
|
||||
|
||||
data.receipt,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await receipt_lines.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await receipt_lines.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await receipt_lines.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipt_lines = await db.receipt_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of receipt_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of receipt_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipt_lines = await db.receipt_lines.findByPk(id, options);
|
||||
|
||||
await receipt_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipt_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipt_lines = await db.receipt_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!receipt_lines) {
|
||||
return receipt_lines;
|
||||
}
|
||||
|
||||
const output = receipt_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.receipt = await receipt_lines.getReceipt({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.item = await receipt_lines.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await receipt_lines.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await receipt_lines.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.receipts,
|
||||
as: 'receipt',
|
||||
|
||||
where: filter.receipt ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.receipt.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
receipt_number: {
|
||||
[Op.or]: filter.receipt.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'receipt_lines',
|
||||
'line_notes',
|
||||
filter.line_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.quantityRange) {
|
||||
const [start, end] = filter.quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.unit_costRange) {
|
||||
const [start, end] = filter.unit_costRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_cost: {
|
||||
...where.unit_cost,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_cost: {
|
||||
...where.unit_cost,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.quality_status) {
|
||||
where = {
|
||||
...where,
|
||||
quality_status: filter.quality_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.receipt_lines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'receipt_lines',
|
||||
'line_notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.receipt_lines.findAll({
|
||||
attributes: [ 'id', 'line_notes' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['line_notes', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.line_notes,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
622
backend/src/db/api/receipts.js
Normal file
622
backend/src/db/api/receipts.js
Normal file
@ -0,0 +1,622 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class ReceiptsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipts = await db.receipts.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
receipt_number: data.receipt_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
received_at: data.received_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
receipt_status: data.receipt_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_document_ref: data.supplier_document_ref
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await receipts.setSupplier( data.supplier || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipts.setReceived_by( data.received_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipts.setTo_location( data.to_location || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.receipts.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: receipts.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return receipts;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const receiptsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
receipt_number: item.receipt_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
received_at: item.received_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
receipt_status: item.receipt_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_document_ref: item.supplier_document_ref
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const receipts = await db.receipts.bulkCreate(receiptsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < receipts.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.receipts.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: receipts[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return receipts;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const receipts = await db.receipts.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.receipt_number !== undefined) updatePayload.receipt_number = data.receipt_number;
|
||||
|
||||
|
||||
if (data.received_at !== undefined) updatePayload.received_at = data.received_at;
|
||||
|
||||
|
||||
if (data.receipt_status !== undefined) updatePayload.receipt_status = data.receipt_status;
|
||||
|
||||
|
||||
if (data.supplier_document_ref !== undefined) updatePayload.supplier_document_ref = data.supplier_document_ref;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await receipts.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.supplier !== undefined) {
|
||||
await receipts.setSupplier(
|
||||
|
||||
data.supplier,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.received_by !== undefined) {
|
||||
await receipts.setReceived_by(
|
||||
|
||||
data.received_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.to_location !== undefined) {
|
||||
await receipts.setTo_location(
|
||||
|
||||
data.to_location,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.receipts.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: receipts.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return receipts;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipts = await db.receipts.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of receipts) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of receipts) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return receipts;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipts = await db.receipts.findByPk(id, options);
|
||||
|
||||
await receipts.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await receipts.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return receipts;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const receipts = await db.receipts.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!receipts) {
|
||||
return receipts;
|
||||
}
|
||||
|
||||
const output = receipts.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.receipt_lines_receipt = await receipts.getReceipt_lines_receipt({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.supplier = await receipts.getSupplier({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.received_by = await receipts.getReceived_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.to_location = await receipts.getTo_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await receipts.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.suppliers,
|
||||
as: 'supplier',
|
||||
|
||||
where: filter.supplier ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.supplier.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
supplier_name: {
|
||||
[Op.or]: filter.supplier.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'received_by',
|
||||
|
||||
where: filter.received_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.received_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.received_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.locations,
|
||||
as: 'to_location',
|
||||
|
||||
where: filter.to_location ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.to_location.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
location_name: {
|
||||
[Op.or]: filter.to_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.receipt_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'receipts',
|
||||
'receipt_number',
|
||||
filter.receipt_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.supplier_document_ref) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'receipts',
|
||||
'supplier_document_ref',
|
||||
filter.supplier_document_ref,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'receipts',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.received_atRange) {
|
||||
const [start, end] = filter.received_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
received_at: {
|
||||
...where.received_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
received_at: {
|
||||
...where.received_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.receipt_status) {
|
||||
where = {
|
||||
...where,
|
||||
receipt_status: filter.receipt_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.receipts.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'receipts',
|
||||
'receipt_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.receipts.findAll({
|
||||
attributes: [ 'id', 'receipt_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['receipt_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.receipt_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
430
backend/src/db/api/roles.js
Normal file
430
backend/src/db/api/roles.js
Normal file
@ -0,0 +1,430 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class RolesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const roles = await db.roles.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
role_customization: data.role_customization
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
await roles.setPermissions(data.permissions || [], {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const rolesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
role_customization: item.role_customization
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const roles = await db.roles.bulkCreate(rolesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const roles = await db.roles.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.role_customization !== undefined) updatePayload.role_customization = data.role_customization;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await roles.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (data.permissions !== undefined) {
|
||||
await roles.setPermissions(data.permissions, { transaction });
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const roles = await db.roles.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of roles) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of roles) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const roles = await db.roles.findByPk(id, options);
|
||||
|
||||
await roles.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await roles.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const roles = await db.roles.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!roles) {
|
||||
return roles;
|
||||
}
|
||||
|
||||
const output = roles.get({plain: true});
|
||||
|
||||
|
||||
output.users_app_role = await roles.getUsers_app_role({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.permissions = await roles.getPermissions({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
{
|
||||
model: db.permissions,
|
||||
as: 'permissions',
|
||||
required: false,
|
||||
},
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'roles',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.role_customization) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'roles',
|
||||
'role_customization',
|
||||
filter.role_customization,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.permissions) {
|
||||
const searchTerms = filter.permissions.split('|');
|
||||
|
||||
include = [
|
||||
{
|
||||
model: db.permissions,
|
||||
as: 'permissions_filter',
|
||||
required: searchTerms.length > 0,
|
||||
where: searchTerms.length > 0 ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
}
|
||||
]
|
||||
} : undefined
|
||||
},
|
||||
...include,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.roles.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'roles',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.roles.findAll({
|
||||
attributes: [ 'id', 'name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
436
backend/src/db/api/routes.js
Normal file
436
backend/src/db/api/routes.js
Normal file
@ -0,0 +1,436 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class RoutesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const routes = await db.routes.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
route_name: data.route_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
route_code: data.route_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
route_status: data.route_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const routesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
route_name: item.route_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
route_code: item.route_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
route_status: item.route_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const routes = await db.routes.bulkCreate(routesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const routes = await db.routes.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.route_name !== undefined) updatePayload.route_name = data.route_name;
|
||||
|
||||
|
||||
if (data.route_code !== undefined) updatePayload.route_code = data.route_code;
|
||||
|
||||
|
||||
if (data.route_status !== undefined) updatePayload.route_status = data.route_status;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await routes.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const routes = await db.routes.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of routes) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of routes) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const routes = await db.routes.findByPk(id, options);
|
||||
|
||||
await routes.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await routes.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const routes = await db.routes.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!routes) {
|
||||
return routes;
|
||||
}
|
||||
|
||||
const output = routes.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.operations_route = await routes.getOperations_route({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_route = await routes.getWork_orders_route({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.route_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'routes',
|
||||
'route_name',
|
||||
filter.route_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.route_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'routes',
|
||||
'route_code',
|
||||
filter.route_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'routes',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.route_status) {
|
||||
where = {
|
||||
...where,
|
||||
route_status: filter.route_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.routes.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'routes',
|
||||
'route_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.routes.findAll({
|
||||
attributes: [ 'id', 'route_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['route_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.route_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
545
backend/src/db/api/shipment_lines.js
Normal file
545
backend/src/db/api/shipment_lines.js
Normal file
@ -0,0 +1,545 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Shipment_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipment_lines = await db.shipment_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
quantity: data.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: data.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await shipment_lines.setShipment( data.shipment || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipment_lines.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipment_lines.setLot( data.lot || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipment_lines.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const shipment_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
quantity: item.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_notes: item.line_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const shipment_lines = await db.shipment_lines.bulkCreate(shipment_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const shipment_lines = await db.shipment_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
|
||||
|
||||
|
||||
if (data.line_notes !== undefined) updatePayload.line_notes = data.line_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await shipment_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.shipment !== undefined) {
|
||||
await shipment_lines.setShipment(
|
||||
|
||||
data.shipment,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await shipment_lines.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.lot !== undefined) {
|
||||
await shipment_lines.setLot(
|
||||
|
||||
data.lot,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await shipment_lines.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipment_lines = await db.shipment_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of shipment_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of shipment_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipment_lines = await db.shipment_lines.findByPk(id, options);
|
||||
|
||||
await shipment_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipment_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipment_lines = await db.shipment_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!shipment_lines) {
|
||||
return shipment_lines;
|
||||
}
|
||||
|
||||
const output = shipment_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.shipment = await shipment_lines.getShipment({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.item = await shipment_lines.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.lot = await shipment_lines.getLot({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await shipment_lines.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.shipments,
|
||||
as: 'shipment',
|
||||
|
||||
where: filter.shipment ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.shipment.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
shipment_number: {
|
||||
[Op.or]: filter.shipment.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.lots,
|
||||
as: 'lot',
|
||||
|
||||
where: filter.lot ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
lot_code: {
|
||||
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.line_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'shipment_lines',
|
||||
'line_notes',
|
||||
filter.line_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.quantityRange) {
|
||||
const [start, end] = filter.quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
quantity: {
|
||||
...where.quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.shipment_lines.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'shipment_lines',
|
||||
'line_notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.shipment_lines.findAll({
|
||||
attributes: [ 'id', 'line_notes' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['line_notes', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.line_notes,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
646
backend/src/db/api/shipments.js
Normal file
646
backend/src/db/api/shipments.js
Normal file
@ -0,0 +1,646 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class ShipmentsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipments = await db.shipments.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
shipment_number: data.shipment_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipped_at: data.shipped_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipment_status: data.shipment_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
carrier: data.carrier
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tracking_number: data.tracking_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await shipments.setCustomer( data.customer || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipments.setFrom_location( data.from_location || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipments.setShipped_by( data.shipped_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.shipments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: shipments.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return shipments;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const shipmentsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
shipment_number: item.shipment_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipped_at: item.shipped_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
shipment_status: item.shipment_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
carrier: item.carrier
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tracking_number: item.tracking_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const shipments = await db.shipments.bulkCreate(shipmentsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < shipments.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.shipments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: shipments[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return shipments;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const shipments = await db.shipments.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.shipment_number !== undefined) updatePayload.shipment_number = data.shipment_number;
|
||||
|
||||
|
||||
if (data.shipped_at !== undefined) updatePayload.shipped_at = data.shipped_at;
|
||||
|
||||
|
||||
if (data.shipment_status !== undefined) updatePayload.shipment_status = data.shipment_status;
|
||||
|
||||
|
||||
if (data.carrier !== undefined) updatePayload.carrier = data.carrier;
|
||||
|
||||
|
||||
if (data.tracking_number !== undefined) updatePayload.tracking_number = data.tracking_number;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await shipments.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.customer !== undefined) {
|
||||
await shipments.setCustomer(
|
||||
|
||||
data.customer,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.from_location !== undefined) {
|
||||
await shipments.setFrom_location(
|
||||
|
||||
data.from_location,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.shipped_by !== undefined) {
|
||||
await shipments.setShipped_by(
|
||||
|
||||
data.shipped_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.shipments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: shipments.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return shipments;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipments = await db.shipments.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of shipments) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of shipments) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return shipments;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipments = await db.shipments.findByPk(id, options);
|
||||
|
||||
await shipments.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await shipments.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return shipments;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const shipments = await db.shipments.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!shipments) {
|
||||
return shipments;
|
||||
}
|
||||
|
||||
const output = shipments.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.shipment_lines_shipment = await shipments.getShipment_lines_shipment({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.customer = await shipments.getCustomer({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.from_location = await shipments.getFrom_location({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.shipped_by = await shipments.getShipped_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await shipments.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.customers,
|
||||
as: 'customer',
|
||||
|
||||
where: filter.customer ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.customer.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
customer_name: {
|
||||
[Op.or]: filter.customer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.locations,
|
||||
as: 'from_location',
|
||||
|
||||
where: filter.from_location ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.from_location.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
location_name: {
|
||||
[Op.or]: filter.from_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'shipped_by',
|
||||
|
||||
where: filter.shipped_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.shipped_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.shipped_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.shipment_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'shipments',
|
||||
'shipment_number',
|
||||
filter.shipment_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.carrier) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'shipments',
|
||||
'carrier',
|
||||
filter.carrier,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.tracking_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'shipments',
|
||||
'tracking_number',
|
||||
filter.tracking_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'shipments',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.shipped_atRange) {
|
||||
const [start, end] = filter.shipped_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
shipped_at: {
|
||||
...where.shipped_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
shipped_at: {
|
||||
...where.shipped_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.shipment_status) {
|
||||
where = {
|
||||
...where,
|
||||
shipment_status: filter.shipment_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.shipments.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'shipments',
|
||||
'shipment_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.shipments.findAll({
|
||||
attributes: [ 'id', 'shipment_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['shipment_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.shipment_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
510
backend/src/db/api/suppliers.js
Normal file
510
backend/src/db/api/suppliers.js
Normal file
@ -0,0 +1,510 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class SuppliersDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const suppliers = await db.suppliers.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
supplier_name: data.supplier_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_code: data.supplier_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contact_name: data.contact_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: data.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: data.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: data.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approved: data.approved
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const suppliersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
supplier_name: item.supplier_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
supplier_code: item.supplier_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contact_name: item.contact_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: item.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: item.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: item.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approved: item.approved
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const suppliers = await db.suppliers.bulkCreate(suppliersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const suppliers = await db.suppliers.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.supplier_name !== undefined) updatePayload.supplier_name = data.supplier_name;
|
||||
|
||||
|
||||
if (data.supplier_code !== undefined) updatePayload.supplier_code = data.supplier_code;
|
||||
|
||||
|
||||
if (data.contact_name !== undefined) updatePayload.contact_name = data.contact_name;
|
||||
|
||||
|
||||
if (data.email !== undefined) updatePayload.email = data.email;
|
||||
|
||||
|
||||
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
||||
|
||||
|
||||
if (data.address !== undefined) updatePayload.address = data.address;
|
||||
|
||||
|
||||
if (data.approved !== undefined) updatePayload.approved = data.approved;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await suppliers.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const suppliers = await db.suppliers.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of suppliers) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of suppliers) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const suppliers = await db.suppliers.findByPk(id, options);
|
||||
|
||||
await suppliers.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await suppliers.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const suppliers = await db.suppliers.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!suppliers) {
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
const output = suppliers.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.lots_supplier = await suppliers.getLots_supplier({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.receipts_supplier = await suppliers.getReceipts_supplier({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.supplier_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'supplier_name',
|
||||
filter.supplier_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.supplier_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'supplier_code',
|
||||
filter.supplier_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.contact_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'contact_name',
|
||||
filter.contact_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'email',
|
||||
filter.email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'phone',
|
||||
filter.phone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'suppliers',
|
||||
'address',
|
||||
filter.address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.approved) {
|
||||
where = {
|
||||
...where,
|
||||
approved: filter.approved,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.suppliers.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'suppliers',
|
||||
'supplier_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.suppliers.findAll({
|
||||
attributes: [ 'id', 'supplier_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['supplier_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.supplier_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
471
backend/src/db/api/uoms.js
Normal file
471
backend/src/db/api/uoms.js
Normal file
@ -0,0 +1,471 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class UomsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const uoms = await db.uoms.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
uom_name: data.uom_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
uom_code: data.uom_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
base_multiplier: data.base_multiplier
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return uoms;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const uomsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
uom_name: item.uom_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
uom_code: item.uom_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
base_multiplier: item.base_multiplier
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const uoms = await db.uoms.bulkCreate(uomsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return uoms;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const uoms = await db.uoms.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.uom_name !== undefined) updatePayload.uom_name = data.uom_name;
|
||||
|
||||
|
||||
if (data.uom_code !== undefined) updatePayload.uom_code = data.uom_code;
|
||||
|
||||
|
||||
if (data.base_multiplier !== undefined) updatePayload.base_multiplier = data.base_multiplier;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await uoms.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return uoms;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const uoms = await db.uoms.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of uoms) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of uoms) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return uoms;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const uoms = await db.uoms.findByPk(id, options);
|
||||
|
||||
await uoms.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await uoms.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return uoms;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const uoms = await db.uoms.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!uoms) {
|
||||
return uoms;
|
||||
}
|
||||
|
||||
const output = uoms.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.items_uom = await uoms.getItems_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_uom = await uoms.getInventory_transactions_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.bom_lines_uom = await uoms.getBom_lines_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_uom = await uoms.getWork_orders_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.material_issue_lines_uom = await uoms.getMaterial_issue_lines_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.receipt_lines_uom = await uoms.getReceipt_lines_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.shipment_lines_uom = await uoms.getShipment_lines_uom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.uom_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'uoms',
|
||||
'uom_name',
|
||||
filter.uom_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.uom_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'uoms',
|
||||
'uom_code',
|
||||
filter.uom_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.base_multiplierRange) {
|
||||
const [start, end] = filter.base_multiplierRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
base_multiplier: {
|
||||
...where.base_multiplier,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
base_multiplier: {
|
||||
...where.base_multiplier,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.uoms.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'uoms',
|
||||
'uom_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.uoms.findAll({
|
||||
attributes: [ 'id', 'uom_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['uom_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.uom_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
998
backend/src/db/api/users.js
Normal file
998
backend/src/db/api/users.js
Normal file
@ -0,0 +1,998 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
const bcrypt = require('bcrypt');
|
||||
const config = require('../../config');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class UsersDBApi {
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const users = await db.users.create(
|
||||
{
|
||||
id: data.data.id || undefined,
|
||||
|
||||
firstName: data.data.firstName
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lastName: data.data.lastName
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phoneNumber: data.data.phoneNumber
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: data.data.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disabled: data.data.disabled
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
password: data.data.password
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
emailVerified: data.data.emailVerified
|
||||
||
|
||||
true
|
||||
|
||||
,
|
||||
|
||||
emailVerificationToken: data.data.emailVerificationToken
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
emailVerificationTokenExpiresAt: data.data.emailVerificationTokenExpiresAt
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
passwordResetToken: data.data.passwordResetToken
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
passwordResetTokenExpiresAt: data.data.passwordResetTokenExpiresAt
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider: data.data.provider
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
if (!data.data.app_role) {
|
||||
const role = await db.roles.findOne({
|
||||
where: { name: 'User' },
|
||||
});
|
||||
if (role) {
|
||||
await users.setApp_role(role, {
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
}else{
|
||||
await users.setApp_role(data.data.app_role || null, {
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
await users.setCustom_permissions(data.data.custom_permissions || [], {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.users.getTableName(),
|
||||
belongsToColumn: 'avatar',
|
||||
belongsToId: users.id,
|
||||
},
|
||||
data.data.avatar,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const usersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
firstName: item.firstName
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lastName: item.lastName
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phoneNumber: item.phoneNumber
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: item.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
disabled: item.disabled
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
password: item.password
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
emailVerified: item.emailVerified
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
emailVerificationToken: item.emailVerificationToken
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
emailVerificationTokenExpiresAt: item.emailVerificationTokenExpiresAt
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
passwordResetToken: item.passwordResetToken
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
passwordResetTokenExpiresAt: item.passwordResetTokenExpiresAt
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider: item.provider
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const users = await db.users.bulkCreate(usersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.users.getTableName(),
|
||||
belongsToColumn: 'avatar',
|
||||
belongsToId: users[i].id,
|
||||
},
|
||||
data[i].avatar,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const users = await db.users.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
if (!data?.app_role) {
|
||||
data.app_role = users?.app_role?.id;
|
||||
}
|
||||
if (!data?.custom_permissions) {
|
||||
data.custom_permissions = users?.custom_permissions?.map(item => item.id);
|
||||
}
|
||||
|
||||
if (data.password) {
|
||||
data.password = bcrypt.hashSync(
|
||||
data.password,
|
||||
config.bcrypt.saltRounds,
|
||||
);
|
||||
} else {
|
||||
data.password = users.password;
|
||||
}
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.firstName !== undefined) updatePayload.firstName = data.firstName;
|
||||
|
||||
|
||||
if (data.lastName !== undefined) updatePayload.lastName = data.lastName;
|
||||
|
||||
|
||||
if (data.phoneNumber !== undefined) updatePayload.phoneNumber = data.phoneNumber;
|
||||
|
||||
|
||||
if (data.email !== undefined) updatePayload.email = data.email;
|
||||
|
||||
|
||||
if (data.disabled !== undefined) updatePayload.disabled = data.disabled;
|
||||
|
||||
|
||||
if (data.password !== undefined) updatePayload.password = data.password;
|
||||
|
||||
|
||||
if (data.emailVerified !== undefined) updatePayload.emailVerified = data.emailVerified;
|
||||
|
||||
else updatePayload.emailVerified = true;
|
||||
|
||||
|
||||
if (data.emailVerificationToken !== undefined) updatePayload.emailVerificationToken = data.emailVerificationToken;
|
||||
|
||||
|
||||
if (data.emailVerificationTokenExpiresAt !== undefined) updatePayload.emailVerificationTokenExpiresAt = data.emailVerificationTokenExpiresAt;
|
||||
|
||||
|
||||
if (data.passwordResetToken !== undefined) updatePayload.passwordResetToken = data.passwordResetToken;
|
||||
|
||||
|
||||
if (data.passwordResetTokenExpiresAt !== undefined) updatePayload.passwordResetTokenExpiresAt = data.passwordResetTokenExpiresAt;
|
||||
|
||||
|
||||
if (data.provider !== undefined) updatePayload.provider = data.provider;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await users.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.app_role !== undefined) {
|
||||
await users.setApp_role(
|
||||
|
||||
data.app_role,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (data.custom_permissions !== undefined) {
|
||||
await users.setCustom_permissions(data.custom_permissions, { transaction });
|
||||
}
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.users.getTableName(),
|
||||
belongsToColumn: 'avatar',
|
||||
belongsToId: users.id,
|
||||
},
|
||||
data.avatar,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const users = await db.users.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of users) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of users) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const users = await db.users.findByPk(id, options);
|
||||
|
||||
await users.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await users.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const users = await db.users.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!users) {
|
||||
return users;
|
||||
}
|
||||
|
||||
const output = users.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_performed_by = await users.getInventory_transactions_performed_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.machine_downtime_events_reported_by = await users.getMachine_downtime_events_reported_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_orders_planner = await users.getWork_orders_planner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.material_issues_issued_by = await users.getMaterial_issues_issued_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.receipts_received_by = await users.getReceipts_received_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.shipments_shipped_by = await users.getShipments_shipped_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_inspections_inspector = await users.getQa_inspections_inspector({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.nonconformances_reported_by = await users.getNonconformances_reported_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
output.nonconformances_owner = await users.getNonconformances_owner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.audit_events_actor = await users.getAudit_events_actor({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.avatar = await users.getAvatar({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.app_role = await users.getApp_role({
|
||||
transaction
|
||||
});
|
||||
|
||||
if (output.app_role) {
|
||||
output.app_role_permissions = await output.app_role.getPermissions({
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
output.custom_permissions = await users.getCustom_permissions({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.roles,
|
||||
as: 'app_role',
|
||||
|
||||
where: filter.app_role ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.app_role.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.app_role.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
model: db.permissions,
|
||||
as: 'custom_permissions',
|
||||
required: false,
|
||||
},
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'avatar',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.firstName) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'firstName',
|
||||
filter.firstName,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.lastName) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'lastName',
|
||||
filter.lastName,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phoneNumber) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'phoneNumber',
|
||||
filter.phoneNumber,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'email',
|
||||
filter.email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.password) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'password',
|
||||
filter.password,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.emailVerificationToken) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'emailVerificationToken',
|
||||
filter.emailVerificationToken,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.passwordResetToken) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'passwordResetToken',
|
||||
filter.passwordResetToken,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.provider) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'users',
|
||||
'provider',
|
||||
filter.provider,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.emailVerificationTokenExpiresAtRange) {
|
||||
const [start, end] = filter.emailVerificationTokenExpiresAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
emailVerificationTokenExpiresAt: {
|
||||
...where.emailVerificationTokenExpiresAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
emailVerificationTokenExpiresAt: {
|
||||
...where.emailVerificationTokenExpiresAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.passwordResetTokenExpiresAtRange) {
|
||||
const [start, end] = filter.passwordResetTokenExpiresAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
passwordResetTokenExpiresAt: {
|
||||
...where.passwordResetTokenExpiresAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
passwordResetTokenExpiresAt: {
|
||||
...where.passwordResetTokenExpiresAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.disabled) {
|
||||
where = {
|
||||
...where,
|
||||
disabled: filter.disabled,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.emailVerified) {
|
||||
where = {
|
||||
...where,
|
||||
emailVerified: filter.emailVerified,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.custom_permissions) {
|
||||
const searchTerms = filter.custom_permissions.split('|');
|
||||
|
||||
include = [
|
||||
{
|
||||
model: db.permissions,
|
||||
as: 'custom_permissions_filter',
|
||||
required: searchTerms.length > 0,
|
||||
where: searchTerms.length > 0 ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
}
|
||||
]
|
||||
} : undefined
|
||||
},
|
||||
...include,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.users.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'users',
|
||||
'firstName',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.users.findAll({
|
||||
attributes: [ 'id', 'firstName' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['firstName', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.firstName,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
static async createFromAuth(data, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const users = await db.users.create(
|
||||
{
|
||||
email: data.email,
|
||||
firstName: data.firstName,
|
||||
authenticationUid: data.authenticationUid,
|
||||
password: data.password,
|
||||
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
const app_role = await db.roles.findOne({
|
||||
where: { name: config.roles?.user || "User" },
|
||||
});
|
||||
if (app_role?.id) {
|
||||
await users.setApp_role(app_role?.id || null, {
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
await users.update(
|
||||
{
|
||||
authenticationUid: users.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
delete users.password;
|
||||
return users;
|
||||
}
|
||||
|
||||
static async updatePassword(id, password, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const users = await db.users.findByPk(id, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await users.update(
|
||||
{
|
||||
password,
|
||||
authenticationUid: id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static async generateEmailVerificationToken(email, options) {
|
||||
return this._generateToken(['emailVerificationToken', 'emailVerificationTokenExpiresAt'], email, options);
|
||||
}
|
||||
|
||||
static async generatePasswordResetToken(email, options) {
|
||||
return this._generateToken(['passwordResetToken', 'passwordResetTokenExpiresAt'], email, options);
|
||||
}
|
||||
|
||||
static async findByPasswordResetToken(token, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
return db.users.findOne(
|
||||
{
|
||||
where: {
|
||||
passwordResetToken: token,
|
||||
passwordResetTokenExpiresAt: {
|
||||
[db.Sequelize.Op.gt]: Date.now(),
|
||||
},
|
||||
},
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
}
|
||||
|
||||
static async findByEmailVerificationToken(
|
||||
token,
|
||||
options,
|
||||
) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
return db.users.findOne(
|
||||
{
|
||||
where: {
|
||||
emailVerificationToken: token,
|
||||
emailVerificationTokenExpiresAt: {
|
||||
[db.Sequelize.Op.gt]: Date.now(),
|
||||
},
|
||||
},
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
}
|
||||
|
||||
static async markEmailVerified(id, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const users = await db.users.findByPk(id, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await users.update(
|
||||
{
|
||||
emailVerified: true,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static async _generateToken(keyNames, email, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const users = await db.users.findOne(
|
||||
{
|
||||
where: { email: email.toLowerCase() },
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
},
|
||||
);
|
||||
|
||||
const token = crypto
|
||||
.randomBytes(20)
|
||||
.toString('hex');
|
||||
const tokenExpiresAt = Date.now() + 360000;
|
||||
|
||||
if(users){
|
||||
await users.update(
|
||||
{
|
||||
[keyNames[0]]: token,
|
||||
[keyNames[1]]: tokenExpiresAt,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{transaction},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
434
backend/src/db/api/warehouses.js
Normal file
434
backend/src/db/api/warehouses.js
Normal file
@ -0,0 +1,434 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class WarehousesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const warehouses = await db.warehouses.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
warehouse_name: data.warehouse_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
warehouse_code: data.warehouse_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: data.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const warehousesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
warehouse_name: item.warehouse_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
warehouse_code: item.warehouse_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: item.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const warehouses = await db.warehouses.bulkCreate(warehousesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const warehouses = await db.warehouses.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.warehouse_name !== undefined) updatePayload.warehouse_name = data.warehouse_name;
|
||||
|
||||
|
||||
if (data.warehouse_code !== undefined) updatePayload.warehouse_code = data.warehouse_code;
|
||||
|
||||
|
||||
if (data.address !== undefined) updatePayload.address = data.address;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await warehouses.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const warehouses = await db.warehouses.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of warehouses) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of warehouses) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const warehouses = await db.warehouses.findByPk(id, options);
|
||||
|
||||
await warehouses.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await warehouses.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const warehouses = await db.warehouses.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!warehouses) {
|
||||
return warehouses;
|
||||
}
|
||||
|
||||
const output = warehouses.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.locations_warehouse = await warehouses.getLocations_warehouse({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.warehouse_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'warehouses',
|
||||
'warehouse_name',
|
||||
filter.warehouse_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.warehouse_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'warehouses',
|
||||
'warehouse_code',
|
||||
filter.warehouse_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'warehouses',
|
||||
'address',
|
||||
filter.address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.warehouses.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'warehouses',
|
||||
'warehouse_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.warehouses.findAll({
|
||||
attributes: [ 'id', 'warehouse_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['warehouse_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.warehouse_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
434
backend/src/db/api/work_centers.js
Normal file
434
backend/src/db/api/work_centers.js
Normal file
@ -0,0 +1,434 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Work_centersDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_centers = await db.work_centers.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
work_center_name: data.work_center_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
work_center_code: data.work_center_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const work_centersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
work_center_name: item.work_center_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
work_center_code: item.work_center_code
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const work_centers = await db.work_centers.bulkCreate(work_centersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const work_centers = await db.work_centers.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.work_center_name !== undefined) updatePayload.work_center_name = data.work_center_name;
|
||||
|
||||
|
||||
if (data.work_center_code !== undefined) updatePayload.work_center_code = data.work_center_code;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await work_centers.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_centers = await db.work_centers.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of work_centers) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of work_centers) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_centers = await db.work_centers.findByPk(id, options);
|
||||
|
||||
await work_centers.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_centers.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_centers = await db.work_centers.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!work_centers) {
|
||||
return work_centers;
|
||||
}
|
||||
|
||||
const output = work_centers.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.production_lines_work_center = await work_centers.getProduction_lines_work_center({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.work_center_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_centers',
|
||||
'work_center_name',
|
||||
filter.work_center_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.work_center_code) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_centers',
|
||||
'work_center_code',
|
||||
filter.work_center_code,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_centers',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.work_centers.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'work_centers',
|
||||
'work_center_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.work_centers.findAll({
|
||||
attributes: [ 'id', 'work_center_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['work_center_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.work_center_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
676
backend/src/db/api/work_order_operations.js
Normal file
676
backend/src/db/api/work_order_operations.js
Normal file
@ -0,0 +1,676 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Work_order_operationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_order_operations = await db.work_order_operations.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
sequence: data.sequence
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
started_at: data.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: data.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
good_quantity: data.good_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scrap_quantity: data.scrap_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
operator_notes: data.operator_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await work_order_operations.setWork_order( data.work_order || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_order_operations.setOperation( data.operation || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_order_operations.setMachine( data.machine || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const work_order_operationsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
sequence: item.sequence
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
started_at: item.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: item.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
good_quantity: item.good_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scrap_quantity: item.scrap_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
operator_notes: item.operator_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const work_order_operations = await db.work_order_operations.bulkCreate(work_order_operationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const work_order_operations = await db.work_order_operations.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.sequence !== undefined) updatePayload.sequence = data.sequence;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
||||
|
||||
|
||||
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
|
||||
|
||||
|
||||
if (data.good_quantity !== undefined) updatePayload.good_quantity = data.good_quantity;
|
||||
|
||||
|
||||
if (data.scrap_quantity !== undefined) updatePayload.scrap_quantity = data.scrap_quantity;
|
||||
|
||||
|
||||
if (data.operator_notes !== undefined) updatePayload.operator_notes = data.operator_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await work_order_operations.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.work_order !== undefined) {
|
||||
await work_order_operations.setWork_order(
|
||||
|
||||
data.work_order,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.operation !== undefined) {
|
||||
await work_order_operations.setOperation(
|
||||
|
||||
data.operation,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.machine !== undefined) {
|
||||
await work_order_operations.setMachine(
|
||||
|
||||
data.machine,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_order_operations = await db.work_order_operations.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of work_order_operations) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of work_order_operations) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_order_operations = await db.work_order_operations.findByPk(id, options);
|
||||
|
||||
await work_order_operations.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_order_operations.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_order_operations = await db.work_order_operations.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!work_order_operations) {
|
||||
return work_order_operations;
|
||||
}
|
||||
|
||||
const output = work_order_operations.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_order = await work_order_operations.getWork_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.operation = await work_order_operations.getOperation({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.machine = await work_order_operations.getMachine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.work_orders,
|
||||
as: 'work_order',
|
||||
|
||||
where: filter.work_order ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
work_order_number: {
|
||||
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.operations,
|
||||
as: 'operation',
|
||||
|
||||
where: filter.operation ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.operation.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
operation_name: {
|
||||
[Op.or]: filter.operation.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.machines,
|
||||
as: 'machine',
|
||||
|
||||
where: filter.machine ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.machine.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
machine_name: {
|
||||
[Op.or]: filter.machine.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.operator_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_order_operations',
|
||||
'operator_notes',
|
||||
filter.operator_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.sequenceRange) {
|
||||
const [start, end] = filter.sequenceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sequence: {
|
||||
...where.sequence,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sequence: {
|
||||
...where.sequence,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.started_atRange) {
|
||||
const [start, end] = filter.started_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
started_at: {
|
||||
...where.started_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
started_at: {
|
||||
...where.started_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.ended_atRange) {
|
||||
const [start, end] = filter.ended_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
ended_at: {
|
||||
...where.ended_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
ended_at: {
|
||||
...where.ended_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.good_quantityRange) {
|
||||
const [start, end] = filter.good_quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
good_quantity: {
|
||||
...where.good_quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
good_quantity: {
|
||||
...where.good_quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.scrap_quantityRange) {
|
||||
const [start, end] = filter.scrap_quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scrap_quantity: {
|
||||
...where.scrap_quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scrap_quantity: {
|
||||
...where.scrap_quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.work_order_operations.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'work_order_operations',
|
||||
'status',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.work_order_operations.findAll({
|
||||
attributes: [ 'id', 'status' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['status', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.status,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
988
backend/src/db/api/work_orders.js
Normal file
988
backend/src/db/api/work_orders.js
Normal file
@ -0,0 +1,988 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
|
||||
const Sequelize = db.Sequelize;
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
module.exports = class Work_ordersDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_orders = await db.work_orders.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
work_order_number: data.work_order_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
planned_quantity: data.planned_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
completed_quantity: data.completed_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_start: data.scheduled_start
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_end: data.scheduled_end
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_start: data.actual_start
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_end: data.actual_end
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await work_orders.setItem( data.item || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setBom( data.bom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setRoute( data.route || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setCustomer( data.customer || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setUom( data.uom || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setProduction_line( data.production_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setPrimary_machine( data.primary_machine || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.setPlanner( data.planner || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.work_orders.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: work_orders.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
|
||||
static async bulkImport(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
// Prepare data - wrapping individual data transformations in a map() method
|
||||
const work_ordersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
work_order_number: item.work_order_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
planned_quantity: item.planned_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
completed_quantity: item.completed_quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_start: item.scheduled_start
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
scheduled_end: item.scheduled_end
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_start: item.actual_start
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_end: item.actual_end
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const work_orders = await db.work_orders.bulkCreate(work_ordersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < work_orders.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.work_orders.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: work_orders[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
|
||||
const work_orders = await db.work_orders.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.work_order_number !== undefined) updatePayload.work_order_number = data.work_order_number;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.planned_quantity !== undefined) updatePayload.planned_quantity = data.planned_quantity;
|
||||
|
||||
|
||||
if (data.completed_quantity !== undefined) updatePayload.completed_quantity = data.completed_quantity;
|
||||
|
||||
|
||||
if (data.scheduled_start !== undefined) updatePayload.scheduled_start = data.scheduled_start;
|
||||
|
||||
|
||||
if (data.scheduled_end !== undefined) updatePayload.scheduled_end = data.scheduled_end;
|
||||
|
||||
|
||||
if (data.actual_start !== undefined) updatePayload.actual_start = data.actual_start;
|
||||
|
||||
|
||||
if (data.actual_end !== undefined) updatePayload.actual_end = data.actual_end;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await work_orders.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.item !== undefined) {
|
||||
await work_orders.setItem(
|
||||
|
||||
data.item,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.bom !== undefined) {
|
||||
await work_orders.setBom(
|
||||
|
||||
data.bom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.route !== undefined) {
|
||||
await work_orders.setRoute(
|
||||
|
||||
data.route,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.customer !== undefined) {
|
||||
await work_orders.setCustomer(
|
||||
|
||||
data.customer,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.uom !== undefined) {
|
||||
await work_orders.setUom(
|
||||
|
||||
data.uom,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.production_line !== undefined) {
|
||||
await work_orders.setProduction_line(
|
||||
|
||||
data.production_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.primary_machine !== undefined) {
|
||||
await work_orders.setPrimary_machine(
|
||||
|
||||
data.primary_machine,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.planner !== undefined) {
|
||||
await work_orders.setPlanner(
|
||||
|
||||
data.planner,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.work_orders.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: work_orders.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_orders = await db.work_orders.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of work_orders) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of work_orders) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_orders = await db.work_orders.findByPk(id, options);
|
||||
|
||||
await work_orders.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await work_orders.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const work_orders = await db.work_orders.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!work_orders) {
|
||||
return work_orders;
|
||||
}
|
||||
|
||||
const output = work_orders.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.inventory_transactions_work_order = await work_orders.getInventory_transactions_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.machine_downtime_events_work_order = await work_orders.getMachine_downtime_events_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.work_order_operations_work_order = await work_orders.getWork_order_operations_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.material_issues_work_order = await work_orders.getMaterial_issues_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.qa_inspections_work_order = await work_orders.getQa_inspections_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.nonconformances_work_order = await work_orders.getNonconformances_work_order({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
output.item = await work_orders.getItem({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.bom = await work_orders.getBom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.route = await work_orders.getRoute({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.customer = await work_orders.getCustomer({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.uom = await work_orders.getUom({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.production_line = await work_orders.getProduction_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.primary_machine = await work_orders.getPrimary_machine({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.planner = await work_orders.getPlanner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await work_orders.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.items,
|
||||
as: 'item',
|
||||
|
||||
where: filter.item ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.item.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
item_name: {
|
||||
[Op.or]: filter.item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.boms,
|
||||
as: 'bom',
|
||||
|
||||
where: filter.bom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.bom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
bom_name: {
|
||||
[Op.or]: filter.bom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.routes,
|
||||
as: 'route',
|
||||
|
||||
where: filter.route ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.route.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
route_name: {
|
||||
[Op.or]: filter.route.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.customers,
|
||||
as: 'customer',
|
||||
|
||||
where: filter.customer ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.customer.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
customer_name: {
|
||||
[Op.or]: filter.customer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.uoms,
|
||||
as: 'uom',
|
||||
|
||||
where: filter.uom ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.uom.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
uom_name: {
|
||||
[Op.or]: filter.uom.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.production_lines,
|
||||
as: 'production_line',
|
||||
|
||||
where: filter.production_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.production_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
line_name: {
|
||||
[Op.or]: filter.production_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.machines,
|
||||
as: 'primary_machine',
|
||||
|
||||
where: filter.primary_machine ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.primary_machine.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
machine_name: {
|
||||
[Op.or]: filter.primary_machine.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'planner',
|
||||
|
||||
where: filter.planner ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.planner.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.planner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.work_order_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_orders',
|
||||
'work_order_number',
|
||||
filter.work_order_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'work_orders',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.planned_quantityRange) {
|
||||
const [start, end] = filter.planned_quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
planned_quantity: {
|
||||
...where.planned_quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
planned_quantity: {
|
||||
...where.planned_quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.completed_quantityRange) {
|
||||
const [start, end] = filter.completed_quantityRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
completed_quantity: {
|
||||
...where.completed_quantity,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
completed_quantity: {
|
||||
...where.completed_quantity,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.scheduled_startRange) {
|
||||
const [start, end] = filter.scheduled_startRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_start: {
|
||||
...where.scheduled_start,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_start: {
|
||||
...where.scheduled_start,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.scheduled_endRange) {
|
||||
const [start, end] = filter.scheduled_endRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_end: {
|
||||
...where.scheduled_end,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
scheduled_end: {
|
||||
...where.scheduled_end,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.actual_startRange) {
|
||||
const [start, end] = filter.actual_startRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_start: {
|
||||
...where.actual_start,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_start: {
|
||||
...where.actual_start,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.actual_endRange) {
|
||||
const [start, end] = filter.actual_endRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_end: {
|
||||
...where.actual_end,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_end: {
|
||||
...where.actual_end,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.createdAtRange) {
|
||||
const [start, end] = filter.createdAtRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
['createdAt']: {
|
||||
...where.createdAt,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const queryOptions = {
|
||||
where,
|
||||
include,
|
||||
distinct: true,
|
||||
order: filter.field && filter.sort
|
||||
? [[filter.field, filter.sort]]
|
||||
: [['createdAt', 'desc']],
|
||||
transaction: options?.transaction,
|
||||
logging: console.log
|
||||
};
|
||||
|
||||
if (!options?.countOnly) {
|
||||
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const { rows, count } = await db.work_orders.findAndCountAll(queryOptions);
|
||||
|
||||
return {
|
||||
rows: options?.countOnly ? [] : rows,
|
||||
count: count
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error executing query:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllAutocomplete(query, limit, offset, ) {
|
||||
let where = {};
|
||||
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'work_orders',
|
||||
'work_order_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.work_orders.findAll({
|
||||
attributes: [ 'id', 'work_order_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['work_order_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.work_order_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
33
backend/src/db/db.config.js
Normal file
33
backend/src/db/db.config.js
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
|
||||
module.exports = {
|
||||
production: {
|
||||
dialect: 'postgres',
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
logging: console.log,
|
||||
seederStorage: 'sequelize',
|
||||
},
|
||||
development: {
|
||||
username: 'postgres',
|
||||
dialect: 'postgres',
|
||||
password: '',
|
||||
database: 'db_manufacturing_erp',
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
logging: console.log,
|
||||
seederStorage: 'sequelize',
|
||||
},
|
||||
dev_stage: {
|
||||
dialect: 'postgres',
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
logging: console.log,
|
||||
seederStorage: 'sequelize',
|
||||
}
|
||||
};
|
||||
8215
backend/src/db/migrations/1775935269492.js
Normal file
8215
backend/src/db/migrations/1775935269492.js
Normal file
File diff suppressed because it is too large
Load Diff
124
backend/src/db/migrations/20260316000000-create-files.js
Normal file
124
backend/src/db/migrations/20260316000000-create-files.js
Normal file
@ -0,0 +1,124 @@
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
const transaction = await queryInterface.sequelize.transaction();
|
||||
|
||||
try {
|
||||
const rows = await queryInterface.sequelize.query(
|
||||
"SELECT to_regclass('public.files') AS regclass_name;",
|
||||
{
|
||||
transaction,
|
||||
type: Sequelize.QueryTypes.SELECT,
|
||||
},
|
||||
);
|
||||
const tableName = rows[0].regclass_name;
|
||||
|
||||
if (tableName) {
|
||||
await transaction.commit();
|
||||
return;
|
||||
}
|
||||
|
||||
await queryInterface.createTable(
|
||||
'files',
|
||||
{
|
||||
id: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
defaultValue: Sequelize.DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
belongsTo: {
|
||||
type: Sequelize.DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
},
|
||||
belongsToId: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
belongsToColumn: {
|
||||
type: Sequelize.DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
},
|
||||
name: {
|
||||
type: Sequelize.DataTypes.STRING(2083),
|
||||
allowNull: false,
|
||||
},
|
||||
sizeInBytes: {
|
||||
type: Sequelize.DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
privateUrl: {
|
||||
type: Sequelize.DataTypes.STRING(2083),
|
||||
allowNull: true,
|
||||
},
|
||||
publicUrl: {
|
||||
type: Sequelize.DataTypes.STRING(2083),
|
||||
allowNull: false,
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
deletedAt: {
|
||||
type: Sequelize.DataTypes.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
createdById: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
allowNull: true,
|
||||
references: {
|
||||
key: 'id',
|
||||
model: 'users',
|
||||
},
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
},
|
||||
updatedById: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
allowNull: true,
|
||||
references: {
|
||||
key: 'id',
|
||||
model: 'users',
|
||||
},
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
},
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
const transaction = await queryInterface.sequelize.transaction();
|
||||
|
||||
try {
|
||||
const rows = await queryInterface.sequelize.query(
|
||||
"SELECT to_regclass('public.files') AS regclass_name;",
|
||||
{
|
||||
transaction,
|
||||
type: Sequelize.QueryTypes.SELECT,
|
||||
},
|
||||
);
|
||||
const tableName = rows[0].regclass_name;
|
||||
|
||||
if (!tableName) {
|
||||
await transaction.commit();
|
||||
return;
|
||||
}
|
||||
|
||||
await queryInterface.dropTable('files', { transaction });
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,77 @@
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
const transaction = await queryInterface.sequelize.transaction();
|
||||
|
||||
try {
|
||||
const rows = await queryInterface.sequelize.query(
|
||||
"SELECT to_regclass('public.\"usersCustom_permissionsPermissions\"') AS regclass_name;",
|
||||
{
|
||||
transaction,
|
||||
type: Sequelize.QueryTypes.SELECT,
|
||||
},
|
||||
);
|
||||
const tableName = rows[0].regclass_name;
|
||||
|
||||
if (tableName) {
|
||||
await transaction.commit();
|
||||
return;
|
||||
}
|
||||
|
||||
await queryInterface.createTable(
|
||||
'usersCustom_permissionsPermissions',
|
||||
{
|
||||
createdAt: {
|
||||
type: Sequelize.DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
users_custom_permissionsId: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
permissionId: {
|
||||
type: Sequelize.DataTypes.UUID,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
|
||||
async down(queryInterface, Sequelize) {
|
||||
const transaction = await queryInterface.sequelize.transaction();
|
||||
|
||||
try {
|
||||
const rows = await queryInterface.sequelize.query(
|
||||
"SELECT to_regclass('public.\"usersCustom_permissionsPermissions\"') AS regclass_name;",
|
||||
{
|
||||
transaction,
|
||||
type: Sequelize.QueryTypes.SELECT,
|
||||
},
|
||||
);
|
||||
const tableName = rows[0].regclass_name;
|
||||
|
||||
if (!tableName) {
|
||||
await transaction.commit();
|
||||
return;
|
||||
}
|
||||
|
||||
await queryInterface.dropTable('usersCustom_permissionsPermissions', { transaction });
|
||||
await transaction.commit();
|
||||
} catch (err) {
|
||||
await transaction.rollback();
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
};
|
||||
173
backend/src/db/models/audit_events.js
Normal file
173
backend/src/db/models/audit_events.js
Normal file
@ -0,0 +1,173 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const audit_events = sequelize.define(
|
||||
'audit_events',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
event_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
event_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"create",
|
||||
|
||||
|
||||
"update",
|
||||
|
||||
|
||||
"delete",
|
||||
|
||||
|
||||
"login",
|
||||
|
||||
|
||||
"export",
|
||||
|
||||
|
||||
"approve",
|
||||
|
||||
|
||||
"status_change"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
event_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
entity_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
record_reference: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
details: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
ip_address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
audit_events.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.audit_events.belongsTo(db.users, {
|
||||
as: 'actor',
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.audit_events.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.audit_events.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return audit_events;
|
||||
};
|
||||
|
||||
|
||||
153
backend/src/db/models/bom_lines.js
Normal file
153
backend/src/db/models/bom_lines.js
Normal file
@ -0,0 +1,153 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const bom_lines = sequelize.define(
|
||||
'bom_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
quantity_per: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
scrap_factor: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
issue_method: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"manual",
|
||||
|
||||
|
||||
"backflush"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
line_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
bom_lines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.bom_lines.belongsTo(db.boms, {
|
||||
as: 'bom',
|
||||
foreignKey: {
|
||||
name: 'bomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.bom_lines.belongsTo(db.items, {
|
||||
as: 'component_item',
|
||||
foreignKey: {
|
||||
name: 'component_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.bom_lines.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.bom_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.bom_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return bom_lines;
|
||||
};
|
||||
|
||||
|
||||
177
backend/src/db/models/boms.js
Normal file
177
backend/src/db/models/boms.js
Normal file
@ -0,0 +1,177 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const boms = sequelize.define(
|
||||
'boms',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
bom_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
bom_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
bom_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"draft",
|
||||
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"obsolete"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
revision: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
effective_from: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
effective_to: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
boms.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.boms.hasMany(db.bom_lines, {
|
||||
as: 'bom_lines_bom',
|
||||
foreignKey: {
|
||||
name: 'bomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.boms.hasMany(db.work_orders, {
|
||||
as: 'work_orders_bom',
|
||||
foreignKey: {
|
||||
name: 'bomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.boms.belongsTo(db.items, {
|
||||
as: 'parent_item',
|
||||
foreignKey: {
|
||||
name: 'parent_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.boms.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.boms.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return boms;
|
||||
};
|
||||
|
||||
|
||||
167
backend/src/db/models/customers.js
Normal file
167
backend/src/db/models/customers.js
Normal file
@ -0,0 +1,167 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const customers = sequelize.define(
|
||||
'customers',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
customer_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
customer_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
contact_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
email: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
phone: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
billing_address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
shipping_address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
customers.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.customers.hasMany(db.work_orders, {
|
||||
as: 'work_orders_customer',
|
||||
foreignKey: {
|
||||
name: 'customerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.customers.hasMany(db.shipments, {
|
||||
as: 'shipments_customer',
|
||||
foreignKey: {
|
||||
name: 'customerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.customers.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.customers.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return customers;
|
||||
};
|
||||
|
||||
|
||||
53
backend/src/db/models/file.js
Normal file
53
backend/src/db/models/file.js
Normal file
@ -0,0 +1,53 @@
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const file = sequelize.define(
|
||||
'file',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
belongsTo: DataTypes.STRING(255),
|
||||
belongsToId: DataTypes.UUID,
|
||||
belongsToColumn: DataTypes.STRING(255),
|
||||
name: {
|
||||
type: DataTypes.STRING(2083),
|
||||
allowNull: false,
|
||||
validate: {
|
||||
notEmpty: true,
|
||||
},
|
||||
},
|
||||
sizeInBytes: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
privateUrl: {
|
||||
type: DataTypes.STRING(2083),
|
||||
allowNull: true,
|
||||
},
|
||||
publicUrl: {
|
||||
type: DataTypes.STRING(2083),
|
||||
allowNull: false,
|
||||
validate: {
|
||||
notEmpty: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
file.associate = (db) => {
|
||||
db.file.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.file.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
return file;
|
||||
};
|
||||
38
backend/src/db/models/index.js
Normal file
38
backend/src/db/models/index.js
Normal file
@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Sequelize = require('sequelize');
|
||||
const basename = path.basename(__filename);
|
||||
const env = process.env.NODE_ENV || 'development';
|
||||
const config = require("../db.config")[env];
|
||||
const db = {};
|
||||
|
||||
let sequelize;
|
||||
console.log(env);
|
||||
if (config.use_env_variable) {
|
||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||
} else {
|
||||
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||
}
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
|
||||
})
|
||||
.forEach(file => {
|
||||
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes)
|
||||
db[model.name] = model;
|
||||
});
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
|
||||
module.exports = db;
|
||||
254
backend/src/db/models/inventory_transactions.js
Normal file
254
backend/src/db/models/inventory_transactions.js
Normal file
@ -0,0 +1,254 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const inventory_transactions = sequelize.define(
|
||||
'inventory_transactions',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
transaction_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
transaction_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
transaction_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"receipt",
|
||||
|
||||
|
||||
"issue_to_production",
|
||||
|
||||
|
||||
"consume",
|
||||
|
||||
|
||||
"produce",
|
||||
|
||||
|
||||
"transfer",
|
||||
|
||||
|
||||
"adjustment",
|
||||
|
||||
|
||||
"scrap",
|
||||
|
||||
|
||||
"cycle_count",
|
||||
|
||||
|
||||
"shipment",
|
||||
|
||||
|
||||
"return_to_stock",
|
||||
|
||||
|
||||
"qa_hold",
|
||||
|
||||
|
||||
"qa_release"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
unit_cost: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
reference: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
reason: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
inventory_transactions.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.inventory_transactions.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.locations, {
|
||||
as: 'from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.locations, {
|
||||
as: 'to_location',
|
||||
foreignKey: {
|
||||
name: 'to_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.qa_inspections, {
|
||||
as: 'qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.users, {
|
||||
as: 'performed_by',
|
||||
foreignKey: {
|
||||
name: 'performed_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.inventory_transactions.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.inventory_transactions.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.inventory_transactions.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.inventory_transactions.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return inventory_transactions;
|
||||
};
|
||||
|
||||
|
||||
131
backend/src/db/models/item_categories.js
Normal file
131
backend/src/db/models/item_categories.js
Normal file
@ -0,0 +1,131 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const item_categories = sequelize.define(
|
||||
'item_categories',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
category_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
category_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
item_categories.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.item_categories.hasMany(db.items, {
|
||||
as: 'items_category',
|
||||
foreignKey: {
|
||||
name: 'categoryId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.item_categories.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.item_categories.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return item_categories;
|
||||
};
|
||||
|
||||
|
||||
324
backend/src/db/models/items.js
Normal file
324
backend/src/db/models/items.js
Normal file
@ -0,0 +1,324 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const items = sequelize.define(
|
||||
'items',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
item_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
item_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
item_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"raw_material",
|
||||
|
||||
|
||||
"wip",
|
||||
|
||||
|
||||
"finished_good",
|
||||
|
||||
|
||||
"consumable",
|
||||
|
||||
|
||||
"spare_part"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
lot_tracked: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
serial_tracked: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
standard_cost: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
default_purchase_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
default_sale_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
shelf_life_days: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
reorder_point: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
reorder_qty: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
items.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.lots, {
|
||||
as: 'lots_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.items.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.items.hasMany(db.boms, {
|
||||
as: 'boms_parent_item',
|
||||
foreignKey: {
|
||||
name: 'parent_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.items.hasMany(db.bom_lines, {
|
||||
as: 'bom_lines_component_item',
|
||||
foreignKey: {
|
||||
name: 'component_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.work_orders, {
|
||||
as: 'work_orders_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.material_issue_lines, {
|
||||
as: 'material_issue_lines_component_item',
|
||||
foreignKey: {
|
||||
name: 'component_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.receipt_lines, {
|
||||
as: 'receipt_lines_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.shipment_lines, {
|
||||
as: 'shipment_lines_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.items.hasMany(db.qa_inspection_plans, {
|
||||
as: 'qa_inspection_plans_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.items.belongsTo(db.item_categories, {
|
||||
as: 'category',
|
||||
foreignKey: {
|
||||
name: 'categoryId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.items.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.items.hasMany(db.file, {
|
||||
as: 'spec_documents',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.items.getTableName(),
|
||||
belongsToColumn: 'spec_documents',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.items.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.items.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return items;
|
||||
};
|
||||
|
||||
|
||||
192
backend/src/db/models/locations.js
Normal file
192
backend/src/db/models/locations.js
Normal file
@ -0,0 +1,192 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const locations = sequelize.define(
|
||||
'locations',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
location_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
location_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
location_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"receiving",
|
||||
|
||||
|
||||
"storage",
|
||||
|
||||
|
||||
"production",
|
||||
|
||||
|
||||
"qa_hold",
|
||||
|
||||
|
||||
"shipping",
|
||||
|
||||
|
||||
"scrap"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
locations.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.locations.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.locations.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_to_location',
|
||||
foreignKey: {
|
||||
name: 'to_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.locations.hasMany(db.material_issue_lines, {
|
||||
as: 'material_issue_lines_from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.locations.hasMany(db.receipts, {
|
||||
as: 'receipts_to_location',
|
||||
foreignKey: {
|
||||
name: 'to_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.locations.hasMany(db.shipments, {
|
||||
as: 'shipments_from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.locations.belongsTo(db.warehouses, {
|
||||
as: 'warehouse',
|
||||
foreignKey: {
|
||||
name: 'warehouseId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.locations.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.locations.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return locations;
|
||||
};
|
||||
|
||||
|
||||
237
backend/src/db/models/lots.js
Normal file
237
backend/src/db/models/lots.js
Normal file
@ -0,0 +1,237 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const lots = sequelize.define(
|
||||
'lots',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
lot_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
supplier_lot_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
received_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
manufactured_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
expires_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
quality_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"quarantine",
|
||||
|
||||
|
||||
"released",
|
||||
|
||||
|
||||
"rejected",
|
||||
|
||||
|
||||
"rework"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
certificate_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
lots.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.material_issue_lines, {
|
||||
as: 'material_issue_lines_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.receipt_lines, {
|
||||
as: 'receipt_lines_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.shipment_lines, {
|
||||
as: 'shipment_lines_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.lots.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.lots.belongsTo(db.suppliers, {
|
||||
as: 'supplier',
|
||||
foreignKey: {
|
||||
name: 'supplierId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.lots.hasMany(db.file, {
|
||||
as: 'certificates',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.lots.getTableName(),
|
||||
belongsToColumn: 'certificates',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.lots.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.lots.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return lots;
|
||||
};
|
||||
|
||||
|
||||
200
backend/src/db/models/machine_downtime_events.js
Normal file
200
backend/src/db/models/machine_downtime_events.js
Normal file
@ -0,0 +1,200 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const machine_downtime_events = sequelize.define(
|
||||
'machine_downtime_events',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
started_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
ended_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
downtime_category: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"breakdown",
|
||||
|
||||
|
||||
"setup_changeover",
|
||||
|
||||
|
||||
"material_shortage",
|
||||
|
||||
|
||||
"quality_hold",
|
||||
|
||||
|
||||
"planned_maintenance",
|
||||
|
||||
|
||||
"unplanned_maintenance",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
severity: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"low",
|
||||
|
||||
|
||||
"medium",
|
||||
|
||||
|
||||
"high",
|
||||
|
||||
|
||||
"critical"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
reason: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
machine_downtime_events.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.machine_downtime_events.belongsTo(db.machines, {
|
||||
as: 'machine',
|
||||
foreignKey: {
|
||||
name: 'machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.machine_downtime_events.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.machine_downtime_events.belongsTo(db.users, {
|
||||
as: 'reported_by',
|
||||
foreignKey: {
|
||||
name: 'reported_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.machine_downtime_events.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.machine_downtime_events.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.machine_downtime_events.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.machine_downtime_events.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return machine_downtime_events;
|
||||
};
|
||||
|
||||
|
||||
244
backend/src/db/models/machines.js
Normal file
244
backend/src/db/models/machines.js
Normal file
@ -0,0 +1,244 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const machines = sequelize.define(
|
||||
'machines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
machine_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
machine_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
machine_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"cnc",
|
||||
|
||||
|
||||
"press",
|
||||
|
||||
|
||||
"molding",
|
||||
|
||||
|
||||
"assembly",
|
||||
|
||||
|
||||
"packaging",
|
||||
|
||||
|
||||
"inspection",
|
||||
|
||||
|
||||
"utility",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"running",
|
||||
|
||||
|
||||
"idle",
|
||||
|
||||
|
||||
"down",
|
||||
|
||||
|
||||
"maintenance",
|
||||
|
||||
|
||||
"setup"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
manufacturer: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
model: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
serial_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
commissioned_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
location_detail: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
machines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.machines.hasMany(db.machine_downtime_events, {
|
||||
as: 'machine_downtime_events_machine',
|
||||
foreignKey: {
|
||||
name: 'machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.machines.hasMany(db.work_orders, {
|
||||
as: 'work_orders_primary_machine',
|
||||
foreignKey: {
|
||||
name: 'primary_machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.machines.hasMany(db.work_order_operations, {
|
||||
as: 'work_order_operations_machine',
|
||||
foreignKey: {
|
||||
name: 'machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.machines.hasMany(db.file, {
|
||||
as: 'manuals',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.machines.getTableName(),
|
||||
belongsToColumn: 'manuals',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.machines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.machines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return machines;
|
||||
};
|
||||
|
||||
|
||||
165
backend/src/db/models/material_issue_lines.js
Normal file
165
backend/src/db/models/material_issue_lines.js
Normal file
@ -0,0 +1,165 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const material_issue_lines = sequelize.define(
|
||||
'material_issue_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
line_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"issued",
|
||||
|
||||
|
||||
"returned",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
line_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
material_issue_lines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.material_issue_lines.belongsTo(db.material_issues, {
|
||||
as: 'material_issue',
|
||||
foreignKey: {
|
||||
name: 'material_issueId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.material_issue_lines.belongsTo(db.items, {
|
||||
as: 'component_item',
|
||||
foreignKey: {
|
||||
name: 'component_itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.material_issue_lines.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.material_issue_lines.belongsTo(db.locations, {
|
||||
as: 'from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.material_issue_lines.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.material_issue_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.material_issue_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return material_issue_lines;
|
||||
};
|
||||
|
||||
|
||||
166
backend/src/db/models/material_issues.js
Normal file
166
backend/src/db/models/material_issues.js
Normal file
@ -0,0 +1,166 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const material_issues = sequelize.define(
|
||||
'material_issues',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
issue_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
issued_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
issue_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"manual_pick",
|
||||
|
||||
|
||||
"backflush",
|
||||
|
||||
|
||||
"return"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
material_issues.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.material_issues.hasMany(db.material_issue_lines, {
|
||||
as: 'material_issue_lines_material_issue',
|
||||
foreignKey: {
|
||||
name: 'material_issueId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.material_issues.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.material_issues.belongsTo(db.users, {
|
||||
as: 'issued_by',
|
||||
foreignKey: {
|
||||
name: 'issued_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.material_issues.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.material_issues.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.material_issues.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.material_issues.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return material_issues;
|
||||
};
|
||||
|
||||
|
||||
306
backend/src/db/models/nonconformances.js
Normal file
306
backend/src/db/models/nonconformances.js
Normal file
@ -0,0 +1,306 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const nonconformances = sequelize.define(
|
||||
'nonconformances',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
nc_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"open",
|
||||
|
||||
|
||||
"under_review",
|
||||
|
||||
|
||||
"contained",
|
||||
|
||||
|
||||
"corrective_action",
|
||||
|
||||
|
||||
"closed",
|
||||
|
||||
|
||||
"void"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
severity: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"minor",
|
||||
|
||||
|
||||
"major",
|
||||
|
||||
|
||||
"critical"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
source: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"incoming",
|
||||
|
||||
|
||||
"in_process",
|
||||
|
||||
|
||||
"final",
|
||||
|
||||
|
||||
"customer",
|
||||
|
||||
|
||||
"audit"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
reported_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
problem_description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
disposition: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"use_as_is",
|
||||
|
||||
|
||||
"rework",
|
||||
|
||||
|
||||
"scrap",
|
||||
|
||||
|
||||
"return_to_supplier",
|
||||
|
||||
|
||||
"sort",
|
||||
|
||||
|
||||
"hold"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
containment_action: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
root_cause: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
corrective_action: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
due_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
closed_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
nonconformances.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.nonconformances.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.qa_inspections, {
|
||||
as: 'qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.users, {
|
||||
as: 'reported_by',
|
||||
foreignKey: {
|
||||
name: 'reported_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.users, {
|
||||
as: 'owner',
|
||||
foreignKey: {
|
||||
name: 'ownerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.nonconformances.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.nonconformances.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.nonconformances.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.nonconformances.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return nonconformances;
|
||||
};
|
||||
|
||||
|
||||
175
backend/src/db/models/operations.js
Normal file
175
backend/src/db/models/operations.js
Normal file
@ -0,0 +1,175 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const operations = sequelize.define(
|
||||
'operations',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
operation_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
operation_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sequence: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
setup_time_minutes: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
run_time_minutes_per_unit: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
labor_time_minutes_per_unit: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
work_instructions: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
operations.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.operations.hasMany(db.work_order_operations, {
|
||||
as: 'work_order_operations_operation',
|
||||
foreignKey: {
|
||||
name: 'operationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.operations.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_operation',
|
||||
foreignKey: {
|
||||
name: 'operationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.operations.belongsTo(db.routes, {
|
||||
as: 'route',
|
||||
foreignKey: {
|
||||
name: 'routeId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.operations.hasMany(db.file, {
|
||||
as: 'instruction_attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.operations.getTableName(),
|
||||
belongsToColumn: 'instruction_attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.operations.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.operations.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return operations;
|
||||
};
|
||||
|
||||
|
||||
99
backend/src/db/models/permissions.js
Normal file
99
backend/src/db/models/permissions.js
Normal file
@ -0,0 +1,99 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const permissions = sequelize.define(
|
||||
'permissions',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
permissions.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.permissions.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.permissions.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return permissions;
|
||||
};
|
||||
|
||||
|
||||
145
backend/src/db/models/production_lines.js
Normal file
145
backend/src/db/models/production_lines.js
Normal file
@ -0,0 +1,145 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const production_lines = sequelize.define(
|
||||
'production_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
line_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
line_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
line_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"inactive"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
production_lines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.production_lines.hasMany(db.work_orders, {
|
||||
as: 'work_orders_production_line',
|
||||
foreignKey: {
|
||||
name: 'production_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.production_lines.belongsTo(db.work_centers, {
|
||||
as: 'work_center',
|
||||
foreignKey: {
|
||||
name: 'work_centerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.production_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.production_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return production_lines;
|
||||
};
|
||||
|
||||
|
||||
204
backend/src/db/models/qa_characteristics.js
Normal file
204
backend/src/db/models/qa_characteristics.js
Normal file
@ -0,0 +1,204 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const qa_characteristics = sequelize.define(
|
||||
'qa_characteristics',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
characteristic_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
characteristic_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
data_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"numeric",
|
||||
|
||||
|
||||
"attribute"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
target_value: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
lower_limit: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
upper_limit: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
attribute_pass_value: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pass",
|
||||
|
||||
|
||||
"ok",
|
||||
|
||||
|
||||
"yes"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
attribute_fail_value: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"fail",
|
||||
|
||||
|
||||
"ng",
|
||||
|
||||
|
||||
"no"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
test_method: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
qa_characteristics.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_characteristics.hasMany(db.qa_results, {
|
||||
as: 'qa_results_characteristic',
|
||||
foreignKey: {
|
||||
name: 'characteristicId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.qa_characteristics.belongsTo(db.qa_inspection_plans, {
|
||||
as: 'inspection_plan',
|
||||
foreignKey: {
|
||||
name: 'inspection_planId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_characteristics.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.qa_characteristics.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return qa_characteristics;
|
||||
};
|
||||
|
||||
|
||||
214
backend/src/db/models/qa_inspection_plans.js
Normal file
214
backend/src/db/models/qa_inspection_plans.js
Normal file
@ -0,0 +1,214 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const qa_inspection_plans = sequelize.define(
|
||||
'qa_inspection_plans',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
plan_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
plan_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
inspection_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"incoming",
|
||||
|
||||
|
||||
"in_process",
|
||||
|
||||
|
||||
"final",
|
||||
|
||||
|
||||
"audit"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
sampling_method: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"100_percent",
|
||||
|
||||
|
||||
"aql",
|
||||
|
||||
|
||||
"fixed_n"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
sample_size: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
plan_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"draft",
|
||||
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"obsolete"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
qa_inspection_plans.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_inspection_plans.hasMany(db.qa_characteristics, {
|
||||
as: 'qa_characteristics_inspection_plan',
|
||||
foreignKey: {
|
||||
name: 'inspection_planId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.qa_inspection_plans.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_inspection_plan',
|
||||
foreignKey: {
|
||||
name: 'inspection_planId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.qa_inspection_plans.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.qa_inspection_plans.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.qa_inspection_plans.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.qa_inspection_plans.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.qa_inspection_plans.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return qa_inspection_plans;
|
||||
};
|
||||
|
||||
|
||||
285
backend/src/db/models/qa_inspections.js
Normal file
285
backend/src/db/models/qa_inspections.js
Normal file
@ -0,0 +1,285 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const qa_inspections = sequelize.define(
|
||||
'qa_inspections',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
inspection_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
inspection_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"incoming",
|
||||
|
||||
|
||||
"in_process",
|
||||
|
||||
|
||||
"final",
|
||||
|
||||
|
||||
"audit"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"planned",
|
||||
|
||||
|
||||
"in_progress",
|
||||
|
||||
|
||||
"passed",
|
||||
|
||||
|
||||
"failed",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
scheduled_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
performed_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sample_size: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
defect_count: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
disposition: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"release",
|
||||
|
||||
|
||||
"hold",
|
||||
|
||||
|
||||
"reject",
|
||||
|
||||
|
||||
"rework"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
summary: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
qa_inspections.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_inspections.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_inspections.hasMany(db.qa_results, {
|
||||
as: 'qa_results_qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.qa_inspections.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.qa_inspections.belongsTo(db.qa_inspection_plans, {
|
||||
as: 'inspection_plan',
|
||||
foreignKey: {
|
||||
name: 'inspection_planId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.operations, {
|
||||
as: 'operation',
|
||||
foreignKey: {
|
||||
name: 'operationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.users, {
|
||||
as: 'inspector',
|
||||
foreignKey: {
|
||||
name: 'inspectorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.qa_inspections.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.qa_inspections.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.qa_inspections.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.qa_inspections.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return qa_inspections;
|
||||
};
|
||||
|
||||
|
||||
151
backend/src/db/models/qa_results.js
Normal file
151
backend/src/db/models/qa_results.js
Normal file
@ -0,0 +1,151 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const qa_results = sequelize.define(
|
||||
'qa_results',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
measured_value: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
attribute_result: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pass",
|
||||
|
||||
|
||||
"fail",
|
||||
|
||||
|
||||
"na"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
within_limits: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
qa_results.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.qa_results.belongsTo(db.qa_inspections, {
|
||||
as: 'qa_inspection',
|
||||
foreignKey: {
|
||||
name: 'qa_inspectionId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.qa_results.belongsTo(db.qa_characteristics, {
|
||||
as: 'characteristic',
|
||||
foreignKey: {
|
||||
name: 'characteristicId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.qa_results.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.qa_results.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return qa_results;
|
||||
};
|
||||
|
||||
|
||||
164
backend/src/db/models/receipt_lines.js
Normal file
164
backend/src/db/models/receipt_lines.js
Normal file
@ -0,0 +1,164 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const receipt_lines = sequelize.define(
|
||||
'receipt_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
unit_cost: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
quality_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"quarantine",
|
||||
|
||||
|
||||
"released",
|
||||
|
||||
|
||||
"rejected"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
line_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
receipt_lines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.receipt_lines.belongsTo(db.receipts, {
|
||||
as: 'receipt',
|
||||
foreignKey: {
|
||||
name: 'receiptId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.receipt_lines.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.receipt_lines.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.receipt_lines.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.receipt_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.receipt_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return receipt_lines;
|
||||
};
|
||||
|
||||
|
||||
187
backend/src/db/models/receipts.js
Normal file
187
backend/src/db/models/receipts.js
Normal file
@ -0,0 +1,187 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const receipts = sequelize.define(
|
||||
'receipts',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
receipt_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
received_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
receipt_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pending",
|
||||
|
||||
|
||||
"received",
|
||||
|
||||
|
||||
"put_away",
|
||||
|
||||
|
||||
"closed",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
supplier_document_ref: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
receipts.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.receipts.hasMany(db.receipt_lines, {
|
||||
as: 'receipt_lines_receipt',
|
||||
foreignKey: {
|
||||
name: 'receiptId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.receipts.belongsTo(db.suppliers, {
|
||||
as: 'supplier',
|
||||
foreignKey: {
|
||||
name: 'supplierId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.receipts.belongsTo(db.users, {
|
||||
as: 'received_by',
|
||||
foreignKey: {
|
||||
name: 'received_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.receipts.belongsTo(db.locations, {
|
||||
as: 'to_location',
|
||||
foreignKey: {
|
||||
name: 'to_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.receipts.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.receipts.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.receipts.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.receipts.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return receipts;
|
||||
};
|
||||
|
||||
|
||||
132
backend/src/db/models/roles.js
Normal file
132
backend/src/db/models/roles.js
Normal file
@ -0,0 +1,132 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const roles = sequelize.define(
|
||||
'roles',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
role_customization: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
roles.associate = (db) => {
|
||||
|
||||
db.roles.belongsToMany(db.permissions, {
|
||||
as: 'permissions',
|
||||
foreignKey: {
|
||||
name: 'roles_permissionsId',
|
||||
},
|
||||
constraints: false,
|
||||
through: 'rolesPermissionsPermissions',
|
||||
});
|
||||
|
||||
db.roles.belongsToMany(db.permissions, {
|
||||
as: 'permissions_filter',
|
||||
foreignKey: {
|
||||
name: 'roles_permissionsId',
|
||||
},
|
||||
constraints: false,
|
||||
through: 'rolesPermissionsPermissions',
|
||||
});
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
db.roles.hasMany(db.users, {
|
||||
as: 'users_app_role',
|
||||
foreignKey: {
|
||||
name: 'app_roleId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.roles.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.roles.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return roles;
|
||||
};
|
||||
|
||||
|
||||
148
backend/src/db/models/routes.js
Normal file
148
backend/src/db/models/routes.js
Normal file
@ -0,0 +1,148 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const routes = sequelize.define(
|
||||
'routes',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
route_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
route_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
route_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"draft",
|
||||
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"obsolete"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
routes.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.routes.hasMany(db.operations, {
|
||||
as: 'operations_route',
|
||||
foreignKey: {
|
||||
name: 'routeId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.routes.hasMany(db.work_orders, {
|
||||
as: 'work_orders_route',
|
||||
foreignKey: {
|
||||
name: 'routeId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.routes.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.routes.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return routes;
|
||||
};
|
||||
|
||||
|
||||
138
backend/src/db/models/shipment_lines.js
Normal file
138
backend/src/db/models/shipment_lines.js
Normal file
@ -0,0 +1,138 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const shipment_lines = sequelize.define(
|
||||
'shipment_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
line_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
shipment_lines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.shipment_lines.belongsTo(db.shipments, {
|
||||
as: 'shipment',
|
||||
foreignKey: {
|
||||
name: 'shipmentId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.shipment_lines.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.shipment_lines.belongsTo(db.lots, {
|
||||
as: 'lot',
|
||||
foreignKey: {
|
||||
name: 'lotId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.shipment_lines.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.shipment_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.shipment_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return shipment_lines;
|
||||
};
|
||||
|
||||
|
||||
194
backend/src/db/models/shipments.js
Normal file
194
backend/src/db/models/shipments.js
Normal file
@ -0,0 +1,194 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const shipments = sequelize.define(
|
||||
'shipments',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
shipment_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
shipped_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
shipment_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"planned",
|
||||
|
||||
|
||||
"packed",
|
||||
|
||||
|
||||
"shipped",
|
||||
|
||||
|
||||
"delivered",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
carrier: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
tracking_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
shipments.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.shipments.hasMany(db.shipment_lines, {
|
||||
as: 'shipment_lines_shipment',
|
||||
foreignKey: {
|
||||
name: 'shipmentId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.shipments.belongsTo(db.customers, {
|
||||
as: 'customer',
|
||||
foreignKey: {
|
||||
name: 'customerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.shipments.belongsTo(db.locations, {
|
||||
as: 'from_location',
|
||||
foreignKey: {
|
||||
name: 'from_locationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.shipments.belongsTo(db.users, {
|
||||
as: 'shipped_by',
|
||||
foreignKey: {
|
||||
name: 'shipped_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.shipments.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.shipments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.shipments.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.shipments.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return shipments;
|
||||
};
|
||||
|
||||
|
||||
160
backend/src/db/models/suppliers.js
Normal file
160
backend/src/db/models/suppliers.js
Normal file
@ -0,0 +1,160 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const suppliers = sequelize.define(
|
||||
'suppliers',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
supplier_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
supplier_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
contact_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
email: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
phone: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
approved: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
suppliers.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.suppliers.hasMany(db.lots, {
|
||||
as: 'lots_supplier',
|
||||
foreignKey: {
|
||||
name: 'supplierId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.suppliers.hasMany(db.receipts, {
|
||||
as: 'receipts_supplier',
|
||||
foreignKey: {
|
||||
name: 'supplierId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.suppliers.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.suppliers.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return suppliers;
|
||||
};
|
||||
|
||||
|
||||
179
backend/src/db/models/uoms.js
Normal file
179
backend/src/db/models/uoms.js
Normal file
@ -0,0 +1,179 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const uoms = sequelize.define(
|
||||
'uoms',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
uom_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
uom_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
base_multiplier: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
uoms.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.items, {
|
||||
as: 'items_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.bom_lines, {
|
||||
as: 'bom_lines_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.work_orders, {
|
||||
as: 'work_orders_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.material_issue_lines, {
|
||||
as: 'material_issue_lines_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.receipt_lines, {
|
||||
as: 'receipt_lines_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.uoms.hasMany(db.shipment_lines, {
|
||||
as: 'shipment_lines_uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.uoms.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.uoms.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return uoms;
|
||||
};
|
||||
|
||||
|
||||
337
backend/src/db/models/users.js
Normal file
337
backend/src/db/models/users.js
Normal file
@ -0,0 +1,337 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const users = sequelize.define(
|
||||
'users',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
firstName: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
lastName: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
phoneNumber: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
email: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
disabled: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
password: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
emailVerified: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
emailVerificationToken: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
emailVerificationTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
passwordResetToken: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
passwordResetTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
provider: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
users.associate = (db) => {
|
||||
|
||||
db.users.belongsToMany(db.permissions, {
|
||||
as: 'custom_permissions',
|
||||
foreignKey: {
|
||||
name: 'users_custom_permissionsId',
|
||||
},
|
||||
constraints: false,
|
||||
through: 'usersCustom_permissionsPermissions',
|
||||
});
|
||||
|
||||
db.users.belongsToMany(db.permissions, {
|
||||
as: 'custom_permissions_filter',
|
||||
foreignKey: {
|
||||
name: 'users_custom_permissionsId',
|
||||
},
|
||||
constraints: false,
|
||||
through: 'usersCustom_permissionsPermissions',
|
||||
});
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_performed_by',
|
||||
foreignKey: {
|
||||
name: 'performed_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.machine_downtime_events, {
|
||||
as: 'machine_downtime_events_reported_by',
|
||||
foreignKey: {
|
||||
name: 'reported_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.work_orders, {
|
||||
as: 'work_orders_planner',
|
||||
foreignKey: {
|
||||
name: 'plannerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.material_issues, {
|
||||
as: 'material_issues_issued_by',
|
||||
foreignKey: {
|
||||
name: 'issued_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.receipts, {
|
||||
as: 'receipts_received_by',
|
||||
foreignKey: {
|
||||
name: 'received_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.shipments, {
|
||||
as: 'shipments_shipped_by',
|
||||
foreignKey: {
|
||||
name: 'shipped_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_inspector',
|
||||
foreignKey: {
|
||||
name: 'inspectorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_reported_by',
|
||||
foreignKey: {
|
||||
name: 'reported_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.users.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_owner',
|
||||
foreignKey: {
|
||||
name: 'ownerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.users.hasMany(db.audit_events, {
|
||||
as: 'audit_events_actor',
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.users.belongsTo(db.roles, {
|
||||
as: 'app_role',
|
||||
foreignKey: {
|
||||
name: 'app_roleId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.users.hasMany(db.file, {
|
||||
as: 'avatar',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.users.getTableName(),
|
||||
belongsToColumn: 'avatar',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.users.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.users.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
users.beforeCreate((users, options) => {
|
||||
users = trimStringFields(users);
|
||||
|
||||
if (users.provider !== providers.LOCAL && Object.values(providers).indexOf(users.provider) > -1) {
|
||||
users.emailVerified = true;
|
||||
|
||||
if (!users.password) {
|
||||
const password = crypto
|
||||
.randomBytes(20)
|
||||
.toString('hex');
|
||||
|
||||
const hashedPassword = bcrypt.hashSync(
|
||||
password,
|
||||
config.bcrypt.saltRounds,
|
||||
);
|
||||
|
||||
users.password = hashedPassword
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
users.beforeUpdate((users, options) => {
|
||||
users = trimStringFields(users);
|
||||
});
|
||||
|
||||
|
||||
return users;
|
||||
};
|
||||
|
||||
|
||||
function trimStringFields(users) {
|
||||
users.email = users.email.trim();
|
||||
|
||||
users.firstName = users.firstName
|
||||
? users.firstName.trim()
|
||||
: null;
|
||||
|
||||
users.lastName = users.lastName
|
||||
? users.lastName.trim()
|
||||
: null;
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
131
backend/src/db/models/warehouses.js
Normal file
131
backend/src/db/models/warehouses.js
Normal file
@ -0,0 +1,131 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const warehouses = sequelize.define(
|
||||
'warehouses',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
warehouse_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
warehouse_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
warehouses.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.warehouses.hasMany(db.locations, {
|
||||
as: 'locations_warehouse',
|
||||
foreignKey: {
|
||||
name: 'warehouseId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.warehouses.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.warehouses.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return warehouses;
|
||||
};
|
||||
|
||||
|
||||
131
backend/src/db/models/work_centers.js
Normal file
131
backend/src/db/models/work_centers.js
Normal file
@ -0,0 +1,131 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const work_centers = sequelize.define(
|
||||
'work_centers',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
work_center_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
work_center_code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
work_centers.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_centers.hasMany(db.production_lines, {
|
||||
as: 'production_lines_work_center',
|
||||
foreignKey: {
|
||||
name: 'work_centerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_centers.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.work_centers.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return work_centers;
|
||||
};
|
||||
|
||||
|
||||
186
backend/src/db/models/work_order_operations.js
Normal file
186
backend/src/db/models/work_order_operations.js
Normal file
@ -0,0 +1,186 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const work_order_operations = sequelize.define(
|
||||
'work_order_operations',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
sequence: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pending",
|
||||
|
||||
|
||||
"ready",
|
||||
|
||||
|
||||
"in_progress",
|
||||
|
||||
|
||||
"blocked",
|
||||
|
||||
|
||||
"completed",
|
||||
|
||||
|
||||
"skipped"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
started_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
ended_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
good_quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
scrap_quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
operator_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
work_order_operations.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.work_order_operations.belongsTo(db.work_orders, {
|
||||
as: 'work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_order_operations.belongsTo(db.operations, {
|
||||
as: 'operation',
|
||||
foreignKey: {
|
||||
name: 'operationId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_order_operations.belongsTo(db.machines, {
|
||||
as: 'machine',
|
||||
foreignKey: {
|
||||
name: 'machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_order_operations.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.work_order_operations.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return work_order_operations;
|
||||
};
|
||||
|
||||
|
||||
301
backend/src/db/models/work_orders.js
Normal file
301
backend/src/db/models/work_orders.js
Normal file
@ -0,0 +1,301 @@
|
||||
const config = require('../../config');
|
||||
const providers = config.providers;
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
const work_orders = sequelize.define(
|
||||
'work_orders',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
work_order_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"planned",
|
||||
|
||||
|
||||
"released",
|
||||
|
||||
|
||||
"in_progress",
|
||||
|
||||
|
||||
"on_hold",
|
||||
|
||||
|
||||
"completed",
|
||||
|
||||
|
||||
"closed",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
planned_quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
completed_quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
scheduled_start: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
scheduled_end: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
actual_start: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
actual_end: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
work_orders.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.inventory_transactions, {
|
||||
as: 'inventory_transactions_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.machine_downtime_events, {
|
||||
as: 'machine_downtime_events_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.work_order_operations, {
|
||||
as: 'work_order_operations_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.material_issues, {
|
||||
as: 'material_issues_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.qa_inspections, {
|
||||
as: 'qa_inspections_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.nonconformances, {
|
||||
as: 'nonconformances_work_order',
|
||||
foreignKey: {
|
||||
name: 'work_orderId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.work_orders.belongsTo(db.items, {
|
||||
as: 'item',
|
||||
foreignKey: {
|
||||
name: 'itemId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.boms, {
|
||||
as: 'bom',
|
||||
foreignKey: {
|
||||
name: 'bomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.routes, {
|
||||
as: 'route',
|
||||
foreignKey: {
|
||||
name: 'routeId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.customers, {
|
||||
as: 'customer',
|
||||
foreignKey: {
|
||||
name: 'customerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.uoms, {
|
||||
as: 'uom',
|
||||
foreignKey: {
|
||||
name: 'uomId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.production_lines, {
|
||||
as: 'production_line',
|
||||
foreignKey: {
|
||||
name: 'production_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.machines, {
|
||||
as: 'primary_machine',
|
||||
foreignKey: {
|
||||
name: 'primary_machineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.users, {
|
||||
as: 'planner',
|
||||
foreignKey: {
|
||||
name: 'plannerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.work_orders.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.work_orders.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.work_orders.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.work_orders.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return work_orders;
|
||||
};
|
||||
|
||||
|
||||
16
backend/src/db/reset.js
Normal file
16
backend/src/db/reset.js
Normal file
@ -0,0 +1,16 @@
|
||||
const db = require('./models');
|
||||
const {execSync} = require("child_process");
|
||||
|
||||
console.log('Resetting Database');
|
||||
|
||||
db.sequelize
|
||||
.sync({ force: true })
|
||||
.then(() => {
|
||||
execSync("sequelize db:seed:all");
|
||||
console.log('OK');
|
||||
process.exit();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
66
backend/src/db/seeders/20200430130759-admin-user.js
Normal file
66
backend/src/db/seeders/20200430130759-admin-user.js
Normal file
@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
const bcrypt = require("bcrypt");
|
||||
const config = require("../../config");
|
||||
|
||||
const ids = [
|
||||
'193bf4b5-9f07-4bd5-9a43-e7e41f3e96af',
|
||||
'af5a87be-8f9c-4630-902a-37a60b7005ba',
|
||||
'5bc531ab-611f-41f3-9373-b7cc5d09c93d',
|
||||
]
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
let admin_hash = bcrypt.hashSync(config.admin_pass, config.bcrypt.saltRounds);
|
||||
let user_hash = bcrypt.hashSync(config.user_pass, config.bcrypt.saltRounds);
|
||||
|
||||
try {
|
||||
await queryInterface.bulkInsert('users', [
|
||||
{
|
||||
id: ids[0],
|
||||
firstName: 'Admin',
|
||||
email: config.admin_email,
|
||||
emailVerified: true,
|
||||
provider: config.providers.LOCAL,
|
||||
password: admin_hash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
},
|
||||
{
|
||||
id: ids[1],
|
||||
firstName: 'John',
|
||||
email: 'john@doe.com',
|
||||
emailVerified: true,
|
||||
provider: config.providers.LOCAL,
|
||||
password: user_hash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
},
|
||||
{
|
||||
id: ids[2],
|
||||
firstName: 'Client',
|
||||
email: 'client@hello.com',
|
||||
emailVerified: true,
|
||||
provider: config.providers.LOCAL,
|
||||
password: user_hash,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
},
|
||||
]);
|
||||
} catch (error) {
|
||||
console.error('Error during bulkInsert:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
try {
|
||||
await queryInterface.bulkDelete('users', {
|
||||
id: {
|
||||
[Sequelize.Op.in]: ids,
|
||||
},
|
||||
}, {});
|
||||
} catch (error) {
|
||||
console.error('Error during bulkDelete:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
3416
backend/src/db/seeders/20200430130760-user-roles.js
Normal file
3416
backend/src/db/seeders/20200430130760-user-roles.js
Normal file
File diff suppressed because it is too large
Load Diff
11670
backend/src/db/seeders/20231127130745-sample-data.js
Normal file
11670
backend/src/db/seeders/20231127130745-sample-data.js
Normal file
File diff suppressed because it is too large
Load Diff
27
backend/src/db/utils.js
Normal file
27
backend/src/db/utils.js
Normal file
@ -0,0 +1,27 @@
|
||||
const validator = require('validator');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const Sequelize = require('./models').Sequelize;
|
||||
|
||||
module.exports = class Utils {
|
||||
static uuid(value) {
|
||||
let id = value;
|
||||
|
||||
if (!validator.isUUID(id)) {
|
||||
id = uuid();
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
static ilike(model, column, value) {
|
||||
return Sequelize.where(
|
||||
Sequelize.fn(
|
||||
'lower',
|
||||
Sequelize.col(`${model}.${column}`),
|
||||
),
|
||||
{
|
||||
[Sequelize.Op.like]: `%${value}%`.toLowerCase(),
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
23
backend/src/helpers.js
Normal file
23
backend/src/helpers.js
Normal file
@ -0,0 +1,23 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const config = require('./config');
|
||||
|
||||
module.exports = class Helpers {
|
||||
static wrapAsync(fn) {
|
||||
return function (req, res, next) {
|
||||
fn(req, res, next).catch(next);
|
||||
};
|
||||
}
|
||||
|
||||
static commonErrorHandler(error, req, res, next) {
|
||||
if ([400, 403, 404].includes(error.code)) {
|
||||
return res.status(error.code).send(error.message);
|
||||
}
|
||||
|
||||
console.error(error);
|
||||
return res.status(500).send(error.message);
|
||||
}
|
||||
|
||||
static jwtSign(data) {
|
||||
return jwt.sign(data, config.secret_key, {expiresIn: '6h'});
|
||||
};
|
||||
};
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user