Initial version
This commit is contained in:
commit
b48affbfb8
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>TSC FleetOS</h2>
|
||||
<p>Multi-tenant PSA/CRM for IT services: sales, projects, support, time tracking and invoicing in FR/EN.</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 @@
|
||||
|
||||
|
||||
# TSC FleetOS
|
||||
|
||||
|
||||
## 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`
|
||||
1
backend/.env
Normal file
1
backend/.env
Normal file
@ -0,0 +1 @@
|
||||
PORT=8080
|
||||
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 @@
|
||||
|
||||
#TSC FleetOS - 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_tsc_fleetos;`
|
||||
|
||||
- Then give that new user privileges to the new database then quit the `psql`.
|
||||
- `postgres=> GRANT ALL PRIVILEGES ON DATABASE db_tsc_fleetos 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": "tscfleetos",
|
||||
"description": "TSC FleetOS - 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});
|
||||
});
|
||||
}
|
||||
81
backend/src/config.js
Normal file
81
backend/src/config.js
Normal file
@ -0,0 +1,81 @@
|
||||
|
||||
|
||||
|
||||
const os = require('os');
|
||||
|
||||
const config = {
|
||||
gcloud: {
|
||||
bucket: "fldemo-files",
|
||||
hash: "afeefb9d49f5b7977577876b99532ac7"
|
||||
},
|
||||
bcrypt: {
|
||||
saltRounds: 12
|
||||
},
|
||||
admin_pass: "252a3d31",
|
||||
user_pass: "6959e42104bd",
|
||||
admin_email: "admin@flatlogic.com",
|
||||
providers: {
|
||||
LOCAL: 'local',
|
||||
GOOGLE: 'google',
|
||||
MICROSOFT: 'microsoft'
|
||||
},
|
||||
secret_key: process.env.SECRET_KEY || '252a3d31-3fce-40fa-b8f8-6959e42104bd',
|
||||
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: 'TSC FleetOS <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: {
|
||||
|
||||
super_admin: 'Super Administrator',
|
||||
|
||||
admin: 'Administrator',
|
||||
|
||||
|
||||
|
||||
user: 'Finance Officer',
|
||||
|
||||
},
|
||||
|
||||
project_uuid: '252a3d31-3fce-40fa-b8f8-6959e42104bd',
|
||||
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 = 'African skyline 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;
|
||||
650
backend/src/db/api/audit_logs.js
Normal file
650
backend/src/db/api/audit_logs.js
Normal file
@ -0,0 +1,650 @@
|
||||
|
||||
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_logsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_logs = await db.audit_logs.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
action: data.action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity: data.entity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity_key: data.entity_key
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ip_address: data.ip_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
user_agent: data.user_agent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
occurred_at: data.occurred_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
details: data.details
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await audit_logs.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await audit_logs.setActor( data.actor || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await audit_logs.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
|
||||
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_logsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
action: item.action
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity: item.entity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
entity_key: item.entity_key
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ip_address: item.ip_address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
user_agent: item.user_agent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
occurred_at: item.occurred_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
details: item.details
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const audit_logs = await db.audit_logs.bulkCreate(audit_logsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const audit_logs = await db.audit_logs.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.action !== undefined) updatePayload.action = data.action;
|
||||
|
||||
|
||||
if (data.entity !== undefined) updatePayload.entity = data.entity;
|
||||
|
||||
|
||||
if (data.entity_key !== undefined) updatePayload.entity_key = data.entity_key;
|
||||
|
||||
|
||||
if (data.ip_address !== undefined) updatePayload.ip_address = data.ip_address;
|
||||
|
||||
|
||||
if (data.user_agent !== undefined) updatePayload.user_agent = data.user_agent;
|
||||
|
||||
|
||||
if (data.occurred_at !== undefined) updatePayload.occurred_at = data.occurred_at;
|
||||
|
||||
|
||||
if (data.details !== undefined) updatePayload.details = data.details;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await audit_logs.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await audit_logs.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.actor !== undefined) {
|
||||
await audit_logs.setActor(
|
||||
|
||||
data.actor,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await audit_logs.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_logs = await db.audit_logs.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of audit_logs) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of audit_logs) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_logs = await db.audit_logs.findByPk(id, options);
|
||||
|
||||
await audit_logs.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await audit_logs.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const audit_logs = await db.audit_logs.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!audit_logs) {
|
||||
return audit_logs;
|
||||
}
|
||||
|
||||
const output = audit_logs.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await audit_logs.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.actor = await audit_logs.getActor({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await audit_logs.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
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}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.entity) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_logs',
|
||||
'entity',
|
||||
filter.entity,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.entity_key) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_logs',
|
||||
'entity_key',
|
||||
filter.entity_key,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.ip_address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_logs',
|
||||
'ip_address',
|
||||
filter.ip_address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.user_agent) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_logs',
|
||||
'user_agent',
|
||||
filter.user_agent,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.details) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'audit_logs',
|
||||
'details',
|
||||
filter.details,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.occurred_atRange) {
|
||||
const [start, end] = filter.occurred_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
occurred_at: {
|
||||
...where.occurred_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
occurred_at: {
|
||||
...where.occurred_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.action) {
|
||||
where = {
|
||||
...where,
|
||||
action: filter.action,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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_logs.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'audit_logs',
|
||||
'entity',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.audit_logs.findAll({
|
||||
attributes: [ 'id', 'entity' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['entity', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.entity,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
818
backend/src/db/api/companies.js
Normal file
818
backend/src/db/api/companies.js
Normal file
@ -0,0 +1,818 @@
|
||||
|
||||
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 CompaniesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const companies = await db.companies.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
legal_name: data.legal_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
trade_name: data.trade_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
rccm: data.rccm
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_id: data.tax_id
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
industry: data.industry
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
size_band: data.size_band
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
annual_revenue: data.annual_revenue
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
website: data.website
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: data.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
city: data.city
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
country: data.country
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lifecycle_stage: data.lifecycle_stage
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await companies.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await companies.setParent_company( data.parent_company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await companies.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return companies;
|
||||
}
|
||||
|
||||
|
||||
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 companiesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
legal_name: item.legal_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
trade_name: item.trade_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
rccm: item.rccm
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_id: item.tax_id
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
industry: item.industry
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
size_band: item.size_band
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
annual_revenue: item.annual_revenue
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
website: item.website
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: item.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
city: item.city
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
country: item.country
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: item.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lifecycle_stage: item.lifecycle_stage
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const companies = await db.companies.bulkCreate(companiesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return companies;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const companies = await db.companies.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.legal_name !== undefined) updatePayload.legal_name = data.legal_name;
|
||||
|
||||
|
||||
if (data.trade_name !== undefined) updatePayload.trade_name = data.trade_name;
|
||||
|
||||
|
||||
if (data.rccm !== undefined) updatePayload.rccm = data.rccm;
|
||||
|
||||
|
||||
if (data.tax_id !== undefined) updatePayload.tax_id = data.tax_id;
|
||||
|
||||
|
||||
if (data.industry !== undefined) updatePayload.industry = data.industry;
|
||||
|
||||
|
||||
if (data.size_band !== undefined) updatePayload.size_band = data.size_band;
|
||||
|
||||
|
||||
if (data.annual_revenue !== undefined) updatePayload.annual_revenue = data.annual_revenue;
|
||||
|
||||
|
||||
if (data.website !== undefined) updatePayload.website = data.website;
|
||||
|
||||
|
||||
if (data.address !== undefined) updatePayload.address = data.address;
|
||||
|
||||
|
||||
if (data.city !== undefined) updatePayload.city = data.city;
|
||||
|
||||
|
||||
if (data.country !== undefined) updatePayload.country = data.country;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
if (data.lifecycle_stage !== undefined) updatePayload.lifecycle_stage = data.lifecycle_stage;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await companies.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await companies.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.parent_company !== undefined) {
|
||||
await companies.setParent_company(
|
||||
|
||||
data.parent_company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await companies.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return companies;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const companies = await db.companies.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of companies) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of companies) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return companies;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const companies = await db.companies.findByPk(id, options);
|
||||
|
||||
await companies.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await companies.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return companies;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const companies = await db.companies.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!companies) {
|
||||
return companies;
|
||||
}
|
||||
|
||||
const output = companies.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.contacts_company = await companies.getContacts_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.deals_company = await companies.getDeals_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.projects_company = await companies.getProjects_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.time_entries_company = await companies.getTime_entries_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.contracts_company = await companies.getContracts_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoices_company = await companies.getInvoices_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.support_tickets_company = await companies.getSupport_tickets_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await companies.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.parent_company = await companies.getParent_company({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await companies.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'parent_company',
|
||||
|
||||
where: filter.parent_company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.parent_company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.parent_company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.legal_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'legal_name',
|
||||
filter.legal_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.trade_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'trade_name',
|
||||
filter.trade_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.rccm) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'rccm',
|
||||
filter.rccm,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.tax_id) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'tax_id',
|
||||
filter.tax_id,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.industry) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'industry',
|
||||
filter.industry,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.website) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'website',
|
||||
filter.website,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'address',
|
||||
filter.address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.city) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'city',
|
||||
filter.city,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.country) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'country',
|
||||
filter.country,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'companies',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.annual_revenueRange) {
|
||||
const [start, end] = filter.annual_revenueRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
annual_revenue: {
|
||||
...where.annual_revenue,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
annual_revenue: {
|
||||
...where.annual_revenue,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.size_band) {
|
||||
where = {
|
||||
...where,
|
||||
size_band: filter.size_band,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.lifecycle_stage) {
|
||||
where = {
|
||||
...where,
|
||||
lifecycle_stage: filter.lifecycle_stage,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.companies.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'companies',
|
||||
'legal_name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.companies.findAll({
|
||||
attributes: [ 'id', 'legal_name' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['legal_name', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.legal_name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
787
backend/src/db/api/contacts.js
Normal file
787
backend/src/db/api/contacts.js
Normal file
@ -0,0 +1,787 @@
|
||||
|
||||
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 ContactsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contacts = await db.contacts.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
first_name: data.first_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
last_name: data.last_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: data.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: data.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
job_title: data.job_title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
whatsapp: data.whatsapp
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
linkedin_url: data.linkedin_url
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
preferred_channel: data.preferred_channel
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allow_email: data.allow_email
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
allow_sms: data.allow_sms
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
allow_whatsapp: data.allow_whatsapp
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
lifecycle_stage: data.lifecycle_stage
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await contacts.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contacts.setCompany( data.company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contacts.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
|
||||
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 contactsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
first_name: item.first_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
last_name: item.last_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: item.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: item.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
job_title: item.job_title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
whatsapp: item.whatsapp
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
linkedin_url: item.linkedin_url
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
preferred_channel: item.preferred_channel
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allow_email: item.allow_email
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
allow_sms: item.allow_sms
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
allow_whatsapp: item.allow_whatsapp
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
lifecycle_stage: item.lifecycle_stage
|
||||
||
|
||||
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 contacts = await db.contacts.bulkCreate(contactsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const contacts = await db.contacts.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.first_name !== undefined) updatePayload.first_name = data.first_name;
|
||||
|
||||
|
||||
if (data.last_name !== undefined) updatePayload.last_name = data.last_name;
|
||||
|
||||
|
||||
if (data.email !== undefined) updatePayload.email = data.email;
|
||||
|
||||
|
||||
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
||||
|
||||
|
||||
if (data.job_title !== undefined) updatePayload.job_title = data.job_title;
|
||||
|
||||
|
||||
if (data.whatsapp !== undefined) updatePayload.whatsapp = data.whatsapp;
|
||||
|
||||
|
||||
if (data.linkedin_url !== undefined) updatePayload.linkedin_url = data.linkedin_url;
|
||||
|
||||
|
||||
if (data.preferred_channel !== undefined) updatePayload.preferred_channel = data.preferred_channel;
|
||||
|
||||
|
||||
if (data.allow_email !== undefined) updatePayload.allow_email = data.allow_email;
|
||||
|
||||
|
||||
if (data.allow_sms !== undefined) updatePayload.allow_sms = data.allow_sms;
|
||||
|
||||
|
||||
if (data.allow_whatsapp !== undefined) updatePayload.allow_whatsapp = data.allow_whatsapp;
|
||||
|
||||
|
||||
if (data.lifecycle_stage !== undefined) updatePayload.lifecycle_stage = data.lifecycle_stage;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await contacts.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await contacts.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.company !== undefined) {
|
||||
await contacts.setCompany(
|
||||
|
||||
data.company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await contacts.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contacts = await db.contacts.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of contacts) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of contacts) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contacts = await db.contacts.findByPk(id, options);
|
||||
|
||||
await contacts.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contacts.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contacts = await db.contacts.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!contacts) {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
const output = contacts.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.deals_primary_contact = await contacts.getDeals_primary_contact({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.support_tickets_contact = await contacts.getSupport_tickets_contact({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.csat_surveys_contact = await contacts.getCsat_surveys_contact({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await contacts.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.company = await contacts.getCompany({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await contacts.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'company',
|
||||
|
||||
where: filter.company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.first_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'first_name',
|
||||
filter.first_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.last_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'last_name',
|
||||
filter.last_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'email',
|
||||
filter.email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'phone',
|
||||
filter.phone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.job_title) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'job_title',
|
||||
filter.job_title,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.whatsapp) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'whatsapp',
|
||||
filter.whatsapp,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.linkedin_url) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'linkedin_url',
|
||||
filter.linkedin_url,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.preferred_channel) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'preferred_channel',
|
||||
filter.preferred_channel,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contacts',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.allow_email) {
|
||||
where = {
|
||||
...where,
|
||||
allow_email: filter.allow_email,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.allow_sms) {
|
||||
where = {
|
||||
...where,
|
||||
allow_sms: filter.allow_sms,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.allow_whatsapp) {
|
||||
where = {
|
||||
...where,
|
||||
allow_whatsapp: filter.allow_whatsapp,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.lifecycle_stage) {
|
||||
where = {
|
||||
...where,
|
||||
lifecycle_stage: filter.lifecycle_stage,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.contacts.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'contacts',
|
||||
'email',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.contacts.findAll({
|
||||
attributes: [ 'id', 'email' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['email', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.email,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
834
backend/src/db/api/contracts.js
Normal file
834
backend/src/db/api/contracts.js
Normal file
@ -0,0 +1,834 @@
|
||||
|
||||
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 ContractsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contracts = await db.contracts.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contract_type: data.contract_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_model: data.billing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: data.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: data.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: data.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
auto_renew: data.auto_renew
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
renewal_notice_days: data.renewal_notice_days
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await contracts.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contracts.setCompany( data.company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contracts.setService_line( data.service_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contracts.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return contracts;
|
||||
}
|
||||
|
||||
|
||||
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 contractsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
contract_type: item.contract_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_model: item.billing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: item.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: item.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: item.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
auto_renew: item.auto_renew
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
renewal_notice_days: item.renewal_notice_days
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.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 contracts = await db.contracts.bulkCreate(contractsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return contracts;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const contracts = await db.contracts.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.contract_type !== undefined) updatePayload.contract_type = data.contract_type;
|
||||
|
||||
|
||||
if (data.billing_model !== undefined) updatePayload.billing_model = data.billing_model;
|
||||
|
||||
|
||||
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
||||
|
||||
|
||||
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
||||
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
|
||||
|
||||
|
||||
if (data.auto_renew !== undefined) updatePayload.auto_renew = data.auto_renew;
|
||||
|
||||
|
||||
if (data.renewal_notice_days !== undefined) updatePayload.renewal_notice_days = data.renewal_notice_days;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await contracts.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await contracts.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.company !== undefined) {
|
||||
await contracts.setCompany(
|
||||
|
||||
data.company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_line !== undefined) {
|
||||
await contracts.setService_line(
|
||||
|
||||
data.service_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await contracts.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return contracts;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contracts = await db.contracts.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of contracts) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of contracts) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return contracts;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contracts = await db.contracts.findByPk(id, options);
|
||||
|
||||
await contracts.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await contracts.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return contracts;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const contracts = await db.contracts.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!contracts) {
|
||||
return contracts;
|
||||
}
|
||||
|
||||
const output = contracts.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.invoices_contract = await contracts.getInvoices_contract({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.support_tickets_contract = await contracts.getSupport_tickets_contract({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await contracts.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.company = await contracts.getCompany({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_line = await contracts.getService_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await contracts.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'company',
|
||||
|
||||
where: filter.company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.service_lines,
|
||||
as: 'service_line',
|
||||
|
||||
where: filter.service_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contracts',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'contracts',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
start_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
end_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.amountRange) {
|
||||
const [start, end] = filter.amountRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.end_atRange) {
|
||||
const [start, end] = filter.end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.renewal_notice_daysRange) {
|
||||
const [start, end] = filter.renewal_notice_daysRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
renewal_notice_days: {
|
||||
...where.renewal_notice_days,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
renewal_notice_days: {
|
||||
...where.renewal_notice_days,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.contract_type) {
|
||||
where = {
|
||||
...where,
|
||||
contract_type: filter.contract_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.billing_model) {
|
||||
where = {
|
||||
...where,
|
||||
billing_model: filter.billing_model,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.currency) {
|
||||
where = {
|
||||
...where,
|
||||
currency: filter.currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.auto_renew) {
|
||||
where = {
|
||||
...where,
|
||||
auto_renew: filter.auto_renew,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.contracts.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'contracts',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.contracts.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
645
backend/src/db/api/csat_surveys.js
Normal file
645
backend/src/db/api/csat_surveys.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 Csat_surveysDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const csat_surveys = await db.csat_surveys.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
score: data.score
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
feedback: data.feedback
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sent_at: data.sent_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
responded_at: data.responded_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await csat_surveys.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await csat_surveys.setTicket( data.ticket || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await csat_surveys.setContact( data.contact || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await csat_surveys.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
|
||||
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 csat_surveysData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
score: item.score
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
feedback: item.feedback
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sent_at: item.sent_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
responded_at: item.responded_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const csat_surveys = await db.csat_surveys.bulkCreate(csat_surveysData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const csat_surveys = await db.csat_surveys.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.score !== undefined) updatePayload.score = data.score;
|
||||
|
||||
|
||||
if (data.feedback !== undefined) updatePayload.feedback = data.feedback;
|
||||
|
||||
|
||||
if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at;
|
||||
|
||||
|
||||
if (data.responded_at !== undefined) updatePayload.responded_at = data.responded_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await csat_surveys.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await csat_surveys.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.ticket !== undefined) {
|
||||
await csat_surveys.setTicket(
|
||||
|
||||
data.ticket,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.contact !== undefined) {
|
||||
await csat_surveys.setContact(
|
||||
|
||||
data.contact,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await csat_surveys.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const csat_surveys = await db.csat_surveys.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of csat_surveys) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of csat_surveys) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const csat_surveys = await db.csat_surveys.findByPk(id, options);
|
||||
|
||||
await csat_surveys.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await csat_surveys.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const csat_surveys = await db.csat_surveys.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!csat_surveys) {
|
||||
return csat_surveys;
|
||||
}
|
||||
|
||||
const output = csat_surveys.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await csat_surveys.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.ticket = await csat_surveys.getTicket({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contact = await csat_surveys.getContact({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await csat_surveys.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.support_tickets,
|
||||
as: 'ticket',
|
||||
|
||||
where: filter.ticket ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.ticket.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
ticket_number: {
|
||||
[Op.or]: filter.ticket.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.contacts,
|
||||
as: 'contact',
|
||||
|
||||
where: filter.contact ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.contact.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
email: {
|
||||
[Op.or]: filter.contact.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.feedback) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'csat_surveys',
|
||||
'feedback',
|
||||
filter.feedback,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.scoreRange) {
|
||||
const [start, end] = filter.scoreRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
score: {
|
||||
...where.score,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
score: {
|
||||
...where.score,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.sent_atRange) {
|
||||
const [start, end] = filter.sent_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sent_at: {
|
||||
...where.sent_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sent_at: {
|
||||
...where.sent_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.responded_atRange) {
|
||||
const [start, end] = filter.responded_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
responded_at: {
|
||||
...where.responded_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
responded_at: {
|
||||
...where.responded_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.csat_surveys.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'csat_surveys',
|
||||
'feedback',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.csat_surveys.findAll({
|
||||
attributes: [ 'id', 'feedback' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['feedback', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.feedback,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
619
backend/src/db/api/deal_stages.js
Normal file
619
backend/src/db/api/deal_stages.js
Normal file
@ -0,0 +1,619 @@
|
||||
|
||||
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 Deal_stagesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deal_stages = await db.deal_stages.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
probability_percent: data.probability_percent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: data.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
is_closed_won: data.is_closed_won
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
is_closed_lost: data.is_closed_lost
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await deal_stages.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deal_stages.setPipeline( data.pipeline || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deal_stages.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
|
||||
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 deal_stagesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
probability_percent: item.probability_percent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: item.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
is_closed_won: item.is_closed_won
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
is_closed_lost: item.is_closed_lost
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const deal_stages = await db.deal_stages.bulkCreate(deal_stagesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const deal_stages = await db.deal_stages.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.probability_percent !== undefined) updatePayload.probability_percent = data.probability_percent;
|
||||
|
||||
|
||||
if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order;
|
||||
|
||||
|
||||
if (data.is_closed_won !== undefined) updatePayload.is_closed_won = data.is_closed_won;
|
||||
|
||||
|
||||
if (data.is_closed_lost !== undefined) updatePayload.is_closed_lost = data.is_closed_lost;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await deal_stages.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await deal_stages.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.pipeline !== undefined) {
|
||||
await deal_stages.setPipeline(
|
||||
|
||||
data.pipeline,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await deal_stages.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deal_stages = await db.deal_stages.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of deal_stages) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of deal_stages) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deal_stages = await db.deal_stages.findByPk(id, options);
|
||||
|
||||
await deal_stages.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deal_stages.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deal_stages = await db.deal_stages.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!deal_stages) {
|
||||
return deal_stages;
|
||||
}
|
||||
|
||||
const output = deal_stages.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.deals_stage = await deal_stages.getDeals_stage({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await deal_stages.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.pipeline = await deal_stages.getPipeline({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await deal_stages.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.pipelines,
|
||||
as: 'pipeline',
|
||||
|
||||
where: filter.pipeline ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.pipeline.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.pipeline.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deal_stages',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.probability_percentRange) {
|
||||
const [start, end] = filter.probability_percentRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
probability_percent: {
|
||||
...where.probability_percent,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
probability_percent: {
|
||||
...where.probability_percent,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.sort_orderRange) {
|
||||
const [start, end] = filter.sort_orderRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.is_closed_won) {
|
||||
where = {
|
||||
...where,
|
||||
is_closed_won: filter.is_closed_won,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.is_closed_lost) {
|
||||
where = {
|
||||
...where,
|
||||
is_closed_lost: filter.is_closed_lost,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.deal_stages.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'deal_stages',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.deal_stages.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
922
backend/src/db/api/deals.js
Normal file
922
backend/src/db/api/deals.js
Normal file
@ -0,0 +1,922 @@
|
||||
|
||||
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 DealsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deals = await db.deals.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: data.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: data.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
expected_close_at: data.expected_close_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
loss_reason: data.loss_reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
won_at: data.won_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lost_at: data.lost_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await deals.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setPipeline( data.pipeline || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setStage( data.stage || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setCompany( data.company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setPrimary_contact( data.primary_contact || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setService_line( data.service_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setOwner( data.owner || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return deals;
|
||||
}
|
||||
|
||||
|
||||
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 dealsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: item.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: item.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
expected_close_at: item.expected_close_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
loss_reason: item.loss_reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
won_at: item.won_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
lost_at: item.lost_at
|
||||
||
|
||||
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 deals = await db.deals.bulkCreate(dealsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return deals;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const deals = await db.deals.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
||||
|
||||
|
||||
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
||||
|
||||
|
||||
if (data.expected_close_at !== undefined) updatePayload.expected_close_at = data.expected_close_at;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.loss_reason !== undefined) updatePayload.loss_reason = data.loss_reason;
|
||||
|
||||
|
||||
if (data.won_at !== undefined) updatePayload.won_at = data.won_at;
|
||||
|
||||
|
||||
if (data.lost_at !== undefined) updatePayload.lost_at = data.lost_at;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await deals.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await deals.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.pipeline !== undefined) {
|
||||
await deals.setPipeline(
|
||||
|
||||
data.pipeline,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.stage !== undefined) {
|
||||
await deals.setStage(
|
||||
|
||||
data.stage,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.company !== undefined) {
|
||||
await deals.setCompany(
|
||||
|
||||
data.company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.primary_contact !== undefined) {
|
||||
await deals.setPrimary_contact(
|
||||
|
||||
data.primary_contact,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_line !== undefined) {
|
||||
await deals.setService_line(
|
||||
|
||||
data.service_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.owner !== undefined) {
|
||||
await deals.setOwner(
|
||||
|
||||
data.owner,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await deals.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return deals;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deals = await db.deals.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of deals) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of deals) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return deals;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deals = await db.deals.findByPk(id, options);
|
||||
|
||||
await deals.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deals.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return deals;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deals = await db.deals.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!deals) {
|
||||
return deals;
|
||||
}
|
||||
|
||||
const output = deals.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.projects_deal = await deals.getProjects_deal({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await deals.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.pipeline = await deals.getPipeline({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.stage = await deals.getStage({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.company = await deals.getCompany({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.primary_contact = await deals.getPrimary_contact({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_line = await deals.getService_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.owner = await deals.getOwner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await deals.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.pipelines,
|
||||
as: 'pipeline',
|
||||
|
||||
where: filter.pipeline ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.pipeline.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.pipeline.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.deal_stages,
|
||||
as: 'stage',
|
||||
|
||||
where: filter.stage ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.stage.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.stage.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'company',
|
||||
|
||||
where: filter.company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.contacts,
|
||||
as: 'primary_contact',
|
||||
|
||||
where: filter.primary_contact ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.primary_contact.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
email: {
|
||||
[Op.or]: filter.primary_contact.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.service_lines,
|
||||
as: 'service_line',
|
||||
|
||||
where: filter.service_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_line.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.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deals',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.loss_reason) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deals',
|
||||
'loss_reason',
|
||||
filter.loss_reason,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deals',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.amountRange) {
|
||||
const [start, end] = filter.amountRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.expected_close_atRange) {
|
||||
const [start, end] = filter.expected_close_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
expected_close_at: {
|
||||
...where.expected_close_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
expected_close_at: {
|
||||
...where.expected_close_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.won_atRange) {
|
||||
const [start, end] = filter.won_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
won_at: {
|
||||
...where.won_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
won_at: {
|
||||
...where.won_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.lost_atRange) {
|
||||
const [start, end] = filter.lost_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
lost_at: {
|
||||
...where.lost_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
lost_at: {
|
||||
...where.lost_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.currency) {
|
||||
where = {
|
||||
...where,
|
||||
currency: filter.currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.deals.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'deals',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.deals.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
644
backend/src/db/api/deliverables.js
Normal file
644
backend/src/db/api/deliverables.js
Normal file
@ -0,0 +1,644 @@
|
||||
|
||||
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 DeliverablesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deliverables = await db.deliverables.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: data.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
client_feedback: data.client_feedback
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await deliverables.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deliverables.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deliverables.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.deliverables.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: deliverables.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
|
||||
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 deliverablesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: item.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
client_feedback: item.client_feedback
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const deliverables = await db.deliverables.bulkCreate(deliverablesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < deliverables.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.deliverables.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: deliverables[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const deliverables = await db.deliverables.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
||||
|
||||
|
||||
if (data.client_feedback !== undefined) updatePayload.client_feedback = data.client_feedback;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await deliverables.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await deliverables.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await deliverables.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await deliverables.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.deliverables.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: deliverables.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deliverables = await db.deliverables.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of deliverables) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of deliverables) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deliverables = await db.deliverables.findByPk(id, options);
|
||||
|
||||
await deliverables.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await deliverables.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const deliverables = await db.deliverables.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!deliverables) {
|
||||
return deliverables;
|
||||
}
|
||||
|
||||
const output = deliverables.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await deliverables.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await deliverables.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await deliverables.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await deliverables.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deliverables',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deliverables',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.client_feedback) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'deliverables',
|
||||
'client_feedback',
|
||||
filter.client_feedback,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.deliverables.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'deliverables',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.deliverables.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
598
backend/src/db/api/email_templates.js
Normal file
598
backend/src/db/api/email_templates.js
Normal file
@ -0,0 +1,598 @@
|
||||
|
||||
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 Email_templatesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const email_templates = await db.email_templates.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
template_key: data.template_key
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subject_fr: data.subject_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subject_en: data.subject_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body_fr: data.body_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body_en: data.body_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await email_templates.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await email_templates.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
|
||||
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 email_templatesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
template_key: item.template_key
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subject_fr: item.subject_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subject_en: item.subject_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body_fr: item.body_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body_en: item.body_en
|
||||
||
|
||||
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 email_templates = await db.email_templates.bulkCreate(email_templatesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const email_templates = await db.email_templates.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.template_key !== undefined) updatePayload.template_key = data.template_key;
|
||||
|
||||
|
||||
if (data.subject_fr !== undefined) updatePayload.subject_fr = data.subject_fr;
|
||||
|
||||
|
||||
if (data.subject_en !== undefined) updatePayload.subject_en = data.subject_en;
|
||||
|
||||
|
||||
if (data.body_fr !== undefined) updatePayload.body_fr = data.body_fr;
|
||||
|
||||
|
||||
if (data.body_en !== undefined) updatePayload.body_en = data.body_en;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await email_templates.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await email_templates.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await email_templates.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const email_templates = await db.email_templates.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of email_templates) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of email_templates) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const email_templates = await db.email_templates.findByPk(id, options);
|
||||
|
||||
await email_templates.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await email_templates.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const email_templates = await db.email_templates.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!email_templates) {
|
||||
return email_templates;
|
||||
}
|
||||
|
||||
const output = email_templates.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await email_templates.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await email_templates.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'email_templates',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.subject_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'email_templates',
|
||||
'subject_fr',
|
||||
filter.subject_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.subject_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'email_templates',
|
||||
'subject_en',
|
||||
filter.subject_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.body_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'email_templates',
|
||||
'body_fr',
|
||||
filter.body_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.body_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'email_templates',
|
||||
'body_en',
|
||||
filter.body_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.template_key) {
|
||||
where = {
|
||||
...where,
|
||||
template_key: filter.template_key,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.email_templates.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'email_templates',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.email_templates.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
764
backend/src/db/api/imports.js
Normal file
764
backend/src/db/api/imports.js
Normal file
@ -0,0 +1,764 @@
|
||||
|
||||
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 ImportsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const imports = await db.imports.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
entity_type: data.entity_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
total_rows: data.total_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
success_rows: data.success_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
failed_rows: data.failed_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
error_report: data.error_report
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
started_at: data.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
finished_at: data.finished_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await imports.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await imports.setRequested_by( data.requested_by || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await imports.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.imports.getTableName(),
|
||||
belongsToColumn: 'source_file',
|
||||
belongsToId: imports.id,
|
||||
},
|
||||
data.source_file,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
|
||||
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 importsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
entity_type: item.entity_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
total_rows: item.total_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
success_rows: item.success_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
failed_rows: item.failed_rows
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
error_report: item.error_report
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
started_at: item.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
finished_at: item.finished_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const imports = await db.imports.bulkCreate(importsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < imports.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.imports.getTableName(),
|
||||
belongsToColumn: 'source_file',
|
||||
belongsToId: imports[i].id,
|
||||
},
|
||||
data[i].source_file,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const imports = await db.imports.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.entity_type !== undefined) updatePayload.entity_type = data.entity_type;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.total_rows !== undefined) updatePayload.total_rows = data.total_rows;
|
||||
|
||||
|
||||
if (data.success_rows !== undefined) updatePayload.success_rows = data.success_rows;
|
||||
|
||||
|
||||
if (data.failed_rows !== undefined) updatePayload.failed_rows = data.failed_rows;
|
||||
|
||||
|
||||
if (data.error_report !== undefined) updatePayload.error_report = data.error_report;
|
||||
|
||||
|
||||
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
||||
|
||||
|
||||
if (data.finished_at !== undefined) updatePayload.finished_at = data.finished_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await imports.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await imports.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.requested_by !== undefined) {
|
||||
await imports.setRequested_by(
|
||||
|
||||
data.requested_by,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await imports.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.imports.getTableName(),
|
||||
belongsToColumn: 'source_file',
|
||||
belongsToId: imports.id,
|
||||
},
|
||||
data.source_file,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const imports = await db.imports.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of imports) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of imports) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const imports = await db.imports.findByPk(id, options);
|
||||
|
||||
await imports.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await imports.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const imports = await db.imports.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!imports) {
|
||||
return imports;
|
||||
}
|
||||
|
||||
const output = imports.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await imports.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.requested_by = await imports.getRequested_by({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.source_file = await imports.getSource_file({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await imports.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'requested_by',
|
||||
|
||||
where: filter.requested_by ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.requested_by.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.requested_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'source_file',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.error_report) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'imports',
|
||||
'error_report',
|
||||
filter.error_report,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.total_rowsRange) {
|
||||
const [start, end] = filter.total_rowsRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
total_rows: {
|
||||
...where.total_rows,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
total_rows: {
|
||||
...where.total_rows,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.success_rowsRange) {
|
||||
const [start, end] = filter.success_rowsRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
success_rows: {
|
||||
...where.success_rows,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
success_rows: {
|
||||
...where.success_rows,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.failed_rowsRange) {
|
||||
const [start, end] = filter.failed_rowsRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
failed_rows: {
|
||||
...where.failed_rows,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
failed_rows: {
|
||||
...where.failed_rows,
|
||||
[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.finished_atRange) {
|
||||
const [start, end] = filter.finished_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
finished_at: {
|
||||
...where.finished_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
finished_at: {
|
||||
...where.finished_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.entity_type) {
|
||||
where = {
|
||||
...where,
|
||||
entity_type: filter.entity_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.imports.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'imports',
|
||||
'entity_type',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.imports.findAll({
|
||||
attributes: [ 'id', 'entity_type' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['entity_type', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.entity_type,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
667
backend/src/db/api/invoice_line_items.js
Normal file
667
backend/src/db/api/invoice_line_items.js
Normal file
@ -0,0 +1,667 @@
|
||||
|
||||
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 Invoice_line_itemsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoice_line_items = await db.invoice_line_items.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quantity: data.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_price: data.unit_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_total: data.line_total
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
taxable: data.taxable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await invoice_line_items.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoice_line_items.setInvoice( data.invoice || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoice_line_items.setProduct( data.product || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoice_line_items.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return invoice_line_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 invoice_line_itemsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
quantity: item.quantity
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_price: item.unit_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
line_total: item.line_total
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
taxable: item.taxable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const invoice_line_items = await db.invoice_line_items.bulkCreate(invoice_line_itemsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return invoice_line_items;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const invoice_line_items = await db.invoice_line_items.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
|
||||
|
||||
|
||||
if (data.unit_price !== undefined) updatePayload.unit_price = data.unit_price;
|
||||
|
||||
|
||||
if (data.line_total !== undefined) updatePayload.line_total = data.line_total;
|
||||
|
||||
|
||||
if (data.taxable !== undefined) updatePayload.taxable = data.taxable;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await invoice_line_items.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await invoice_line_items.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.invoice !== undefined) {
|
||||
await invoice_line_items.setInvoice(
|
||||
|
||||
data.invoice,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.product !== undefined) {
|
||||
await invoice_line_items.setProduct(
|
||||
|
||||
data.product,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await invoice_line_items.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return invoice_line_items;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoice_line_items = await db.invoice_line_items.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of invoice_line_items) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of invoice_line_items) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return invoice_line_items;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoice_line_items = await db.invoice_line_items.findByPk(id, options);
|
||||
|
||||
await invoice_line_items.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoice_line_items.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return invoice_line_items;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoice_line_items = await db.invoice_line_items.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!invoice_line_items) {
|
||||
return invoice_line_items;
|
||||
}
|
||||
|
||||
const output = invoice_line_items.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await invoice_line_items.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoice = await invoice_line_items.getInvoice({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.product = await invoice_line_items.getProduct({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await invoice_line_items.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.invoices,
|
||||
as: 'invoice',
|
||||
|
||||
where: filter.invoice ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.invoice.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
invoice_number: {
|
||||
[Op.or]: filter.invoice.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.products,
|
||||
as: 'product',
|
||||
|
||||
where: filter.product ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'invoice_line_items',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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_priceRange) {
|
||||
const [start, end] = filter.unit_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_price: {
|
||||
...where.unit_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_price: {
|
||||
...where.unit_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.line_totalRange) {
|
||||
const [start, end] = filter.line_totalRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
line_total: {
|
||||
...where.line_total,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
line_total: {
|
||||
...where.line_total,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.taxable) {
|
||||
where = {
|
||||
...where,
|
||||
taxable: filter.taxable,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.invoice_line_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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'invoice_line_items',
|
||||
'description',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.invoice_line_items.findAll({
|
||||
attributes: [ 'id', 'description' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['description', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.description,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
988
backend/src/db/api/invoices.js
Normal file
988
backend/src/db/api/invoices.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 InvoicesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoices = await db.invoices.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
invoice_number: data.invoice_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
type: data.type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_at: data.issue_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: data.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: data.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subtotal: data.subtotal
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_rate_percent: data.tax_rate_percent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_amount: data.tax_amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
total: data.total
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount_paid: data.amount_paid
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
public_notes: data.public_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
internal_notes: data.internal_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await invoices.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoices.setCompany( data.company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoices.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoices.setContract( data.contract || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoices.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.invoices.getTableName(),
|
||||
belongsToColumn: 'pdf_file',
|
||||
belongsToId: invoices.id,
|
||||
},
|
||||
data.pdf_file,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
|
||||
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 invoicesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
invoice_number: item.invoice_number
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
type: item.type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
issue_at: item.issue_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: item.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: item.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subtotal: item.subtotal
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_rate_percent: item.tax_rate_percent
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tax_amount: item.tax_amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
total: item.total
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount_paid: item.amount_paid
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
public_notes: item.public_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
internal_notes: item.internal_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const invoices = await db.invoices.bulkCreate(invoicesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < invoices.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.invoices.getTableName(),
|
||||
belongsToColumn: 'pdf_file',
|
||||
belongsToId: invoices[i].id,
|
||||
},
|
||||
data[i].pdf_file,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const invoices = await db.invoices.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.invoice_number !== undefined) updatePayload.invoice_number = data.invoice_number;
|
||||
|
||||
|
||||
if (data.type !== undefined) updatePayload.type = data.type;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.issue_at !== undefined) updatePayload.issue_at = data.issue_at;
|
||||
|
||||
|
||||
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
||||
|
||||
|
||||
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
||||
|
||||
|
||||
if (data.subtotal !== undefined) updatePayload.subtotal = data.subtotal;
|
||||
|
||||
|
||||
if (data.tax_rate_percent !== undefined) updatePayload.tax_rate_percent = data.tax_rate_percent;
|
||||
|
||||
|
||||
if (data.tax_amount !== undefined) updatePayload.tax_amount = data.tax_amount;
|
||||
|
||||
|
||||
if (data.total !== undefined) updatePayload.total = data.total;
|
||||
|
||||
|
||||
if (data.amount_paid !== undefined) updatePayload.amount_paid = data.amount_paid;
|
||||
|
||||
|
||||
if (data.public_notes !== undefined) updatePayload.public_notes = data.public_notes;
|
||||
|
||||
|
||||
if (data.internal_notes !== undefined) updatePayload.internal_notes = data.internal_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await invoices.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await invoices.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.company !== undefined) {
|
||||
await invoices.setCompany(
|
||||
|
||||
data.company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await invoices.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.contract !== undefined) {
|
||||
await invoices.setContract(
|
||||
|
||||
data.contract,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await invoices.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.invoices.getTableName(),
|
||||
belongsToColumn: 'pdf_file',
|
||||
belongsToId: invoices.id,
|
||||
},
|
||||
data.pdf_file,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoices = await db.invoices.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of invoices) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of invoices) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoices = await db.invoices.findByPk(id, options);
|
||||
|
||||
await invoices.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await invoices.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const invoices = await db.invoices.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!invoices) {
|
||||
return invoices;
|
||||
}
|
||||
|
||||
const output = invoices.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.invoice_line_items_invoice = await invoices.getInvoice_line_items_invoice({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.payments_invoice = await invoices.getPayments_invoice({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await invoices.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.company = await invoices.getCompany({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await invoices.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contract = await invoices.getContract({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.pdf_file = await invoices.getPdf_file({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await invoices.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'company',
|
||||
|
||||
where: filter.company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.contracts,
|
||||
as: 'contract',
|
||||
|
||||
where: filter.contract ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.contract.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.contract.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'pdf_file',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.invoice_number) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'invoices',
|
||||
'invoice_number',
|
||||
filter.invoice_number,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.public_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'invoices',
|
||||
'public_notes',
|
||||
filter.public_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.internal_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'invoices',
|
||||
'internal_notes',
|
||||
filter.internal_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.issue_atRange) {
|
||||
const [start, end] = filter.issue_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
issue_at: {
|
||||
...where.issue_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
issue_at: {
|
||||
...where.issue_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.subtotalRange) {
|
||||
const [start, end] = filter.subtotalRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
subtotal: {
|
||||
...where.subtotal,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
subtotal: {
|
||||
...where.subtotal,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.tax_rate_percentRange) {
|
||||
const [start, end] = filter.tax_rate_percentRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
tax_rate_percent: {
|
||||
...where.tax_rate_percent,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
tax_rate_percent: {
|
||||
...where.tax_rate_percent,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.tax_amountRange) {
|
||||
const [start, end] = filter.tax_amountRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
tax_amount: {
|
||||
...where.tax_amount,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
tax_amount: {
|
||||
...where.tax_amount,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.totalRange) {
|
||||
const [start, end] = filter.totalRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
total: {
|
||||
...where.total,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
total: {
|
||||
...where.total,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.amount_paidRange) {
|
||||
const [start, end] = filter.amount_paidRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount_paid: {
|
||||
...where.amount_paid,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount_paid: {
|
||||
...where.amount_paid,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.type) {
|
||||
where = {
|
||||
...where,
|
||||
type: filter.type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.currency) {
|
||||
where = {
|
||||
...where,
|
||||
currency: filter.currency,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.invoices.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'invoices',
|
||||
'invoice_number',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.invoices.findAll({
|
||||
attributes: [ 'id', 'invoice_number' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['invoice_number', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.invoice_number,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
744
backend/src/db/api/leads.js
Normal file
744
backend/src/db/api/leads.js
Normal file
@ -0,0 +1,744 @@
|
||||
|
||||
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 LeadsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leads = await db.leads.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
full_name: data.full_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: data.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: data.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
company_name: data.company_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
message: data.message
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
source: data.source
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
score: data.score
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
captured_at: data.captured_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await leads.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leads.setService_interest( data.service_interest || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leads.setOwner( data.owner || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leads.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return leads;
|
||||
}
|
||||
|
||||
|
||||
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 leadsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
full_name: item.full_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
email: item.email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: item.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
company_name: item.company_name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
message: item.message
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
source: item.source
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
score: item.score
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
captured_at: item.captured_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const leads = await db.leads.bulkCreate(leadsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return leads;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const leads = await db.leads.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.full_name !== undefined) updatePayload.full_name = data.full_name;
|
||||
|
||||
|
||||
if (data.email !== undefined) updatePayload.email = data.email;
|
||||
|
||||
|
||||
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
||||
|
||||
|
||||
if (data.company_name !== undefined) updatePayload.company_name = data.company_name;
|
||||
|
||||
|
||||
if (data.message !== undefined) updatePayload.message = data.message;
|
||||
|
||||
|
||||
if (data.source !== undefined) updatePayload.source = data.source;
|
||||
|
||||
|
||||
if (data.score !== undefined) updatePayload.score = data.score;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await leads.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await leads.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_interest !== undefined) {
|
||||
await leads.setService_interest(
|
||||
|
||||
data.service_interest,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.owner !== undefined) {
|
||||
await leads.setOwner(
|
||||
|
||||
data.owner,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await leads.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return leads;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leads = await db.leads.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of leads) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of leads) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return leads;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leads = await db.leads.findByPk(id, options);
|
||||
|
||||
await leads.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leads.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return leads;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leads = await db.leads.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!leads) {
|
||||
return leads;
|
||||
}
|
||||
|
||||
const output = leads.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await leads.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_interest = await leads.getService_interest({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.owner = await leads.getOwner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await leads.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.services,
|
||||
as: 'service_interest',
|
||||
|
||||
where: filter.service_interest ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_interest.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_interest.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.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.full_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leads',
|
||||
'full_name',
|
||||
filter.full_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leads',
|
||||
'email',
|
||||
filter.email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leads',
|
||||
'phone',
|
||||
filter.phone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.company_name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leads',
|
||||
'company_name',
|
||||
filter.company_name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.message) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leads',
|
||||
'message',
|
||||
filter.message,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.scoreRange) {
|
||||
const [start, end] = filter.scoreRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
score: {
|
||||
...where.score,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
score: {
|
||||
...where.score,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.captured_atRange) {
|
||||
const [start, end] = filter.captured_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
captured_at: {
|
||||
...where.captured_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
captured_at: {
|
||||
...where.captured_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.source) {
|
||||
where = {
|
||||
...where,
|
||||
source: filter.source,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.leads.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'leads',
|
||||
'email',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.leads.findAll({
|
||||
attributes: [ 'id', 'email' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['email', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.email,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
666
backend/src/db/api/leave_requests.js
Normal file
666
backend/src/db/api/leave_requests.js
Normal file
@ -0,0 +1,666 @@
|
||||
|
||||
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 Leave_requestsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leave_requests = await db.leave_requests.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: data.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
leave_type: data.leave_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reason: data.reason
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await leave_requests.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leave_requests.setTeam_member( data.team_member || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leave_requests.setApprover( data.approver || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leave_requests.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
|
||||
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 leave_requestsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: item.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
leave_type: item.leave_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
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 leave_requests = await db.leave_requests.bulkCreate(leave_requestsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const leave_requests = await db.leave_requests.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
|
||||
|
||||
|
||||
if (data.leave_type !== undefined) updatePayload.leave_type = data.leave_type;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.reason !== undefined) updatePayload.reason = data.reason;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await leave_requests.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await leave_requests.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.team_member !== undefined) {
|
||||
await leave_requests.setTeam_member(
|
||||
|
||||
data.team_member,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.approver !== undefined) {
|
||||
await leave_requests.setApprover(
|
||||
|
||||
data.approver,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await leave_requests.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leave_requests = await db.leave_requests.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of leave_requests) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of leave_requests) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leave_requests = await db.leave_requests.findByPk(id, options);
|
||||
|
||||
await leave_requests.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await leave_requests.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const leave_requests = await db.leave_requests.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!leave_requests) {
|
||||
return leave_requests;
|
||||
}
|
||||
|
||||
const output = leave_requests.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await leave_requests.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_member = await leave_requests.getTeam_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.approver = await leave_requests.getApprover({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await leave_requests.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.team_members,
|
||||
as: 'team_member',
|
||||
|
||||
where: filter.team_member ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.team_member.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
notes: {
|
||||
[Op.or]: filter.team_member.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'approver',
|
||||
|
||||
where: filter.approver ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.approver.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.approver.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.reason) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'leave_requests',
|
||||
'reason',
|
||||
filter.reason,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
start_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
end_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.end_atRange) {
|
||||
const [start, end] = filter.end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.leave_type) {
|
||||
where = {
|
||||
...where,
|
||||
leave_type: filter.leave_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.leave_requests.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'leave_requests',
|
||||
'reason',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.leave_requests.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
674
backend/src/db/api/milestones.js
Normal file
674
backend/src/db/api/milestones.js
Normal file
@ -0,0 +1,674 @@
|
||||
|
||||
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 MilestonesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const milestones = await db.milestones.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: data.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: data.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await milestones.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await milestones.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await milestones.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return milestones;
|
||||
}
|
||||
|
||||
|
||||
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 milestonesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: item.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: item.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const milestones = await db.milestones.bulkCreate(milestonesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return milestones;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const milestones = await db.milestones.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await milestones.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await milestones.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await milestones.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await milestones.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return milestones;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const milestones = await db.milestones.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of milestones) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of milestones) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return milestones;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const milestones = await db.milestones.findByPk(id, options);
|
||||
|
||||
await milestones.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await milestones.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return milestones;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const milestones = await db.milestones.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!milestones) {
|
||||
return milestones;
|
||||
}
|
||||
|
||||
const output = milestones.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tasks_milestone = await milestones.getTasks_milestone({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await milestones.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await milestones.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await milestones.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'milestones',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'milestones',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
start_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
due_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_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.sort_orderRange) {
|
||||
const [start, end] = filter.sort_orderRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[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.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.milestones.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'milestones',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.milestones.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
681
backend/src/db/api/notifications.js
Normal file
681
backend/src/db/api/notifications.js
Normal file
@ -0,0 +1,681 @@
|
||||
|
||||
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 NotificationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const notifications = await db.notifications.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
channel: data.channel
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_type: data.event_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
title: data.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body: data.body
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
link_url: data.link_url
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
read: data.read
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
sent_at: data.sent_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
read_at: data.read_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await notifications.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await notifications.setUser( data.user || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await notifications.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
|
||||
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 notificationsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
channel: item.channel
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
event_type: item.event_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
title: item.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body: item.body
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
link_url: item.link_url
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
read: item.read
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
sent_at: item.sent_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
read_at: item.read_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const notifications = await db.notifications.bulkCreate(notificationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const notifications = await db.notifications.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.channel !== undefined) updatePayload.channel = data.channel;
|
||||
|
||||
|
||||
if (data.event_type !== undefined) updatePayload.event_type = data.event_type;
|
||||
|
||||
|
||||
if (data.title !== undefined) updatePayload.title = data.title;
|
||||
|
||||
|
||||
if (data.body !== undefined) updatePayload.body = data.body;
|
||||
|
||||
|
||||
if (data.link_url !== undefined) updatePayload.link_url = data.link_url;
|
||||
|
||||
|
||||
if (data.read !== undefined) updatePayload.read = data.read;
|
||||
|
||||
|
||||
if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at;
|
||||
|
||||
|
||||
if (data.read_at !== undefined) updatePayload.read_at = data.read_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await notifications.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await notifications.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.user !== undefined) {
|
||||
await notifications.setUser(
|
||||
|
||||
data.user,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await notifications.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const notifications = await db.notifications.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of notifications) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of notifications) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const notifications = await db.notifications.findByPk(id, options);
|
||||
|
||||
await notifications.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await notifications.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return notifications;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const notifications = await db.notifications.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!notifications) {
|
||||
return notifications;
|
||||
}
|
||||
|
||||
const output = notifications.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await notifications.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.user = await notifications.getUser({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await notifications.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'user',
|
||||
|
||||
where: filter.user ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.title) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'notifications',
|
||||
'title',
|
||||
filter.title,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.body) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'notifications',
|
||||
'body',
|
||||
filter.body,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.link_url) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'notifications',
|
||||
'link_url',
|
||||
filter.link_url,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.sent_atRange) {
|
||||
const [start, end] = filter.sent_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sent_at: {
|
||||
...where.sent_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sent_at: {
|
||||
...where.sent_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.read_atRange) {
|
||||
const [start, end] = filter.read_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
read_at: {
|
||||
...where.read_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
read_at: {
|
||||
...where.read_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.channel) {
|
||||
where = {
|
||||
...where,
|
||||
channel: filter.channel,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.event_type) {
|
||||
where = {
|
||||
...where,
|
||||
event_type: filter.event_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.read) {
|
||||
where = {
|
||||
...where,
|
||||
read: filter.read,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.notifications.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'notifications',
|
||||
'title',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.notifications.findAll({
|
||||
attributes: [ 'id', 'title' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['title', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.title,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
546
backend/src/db/api/organizations.js
Normal file
546
backend/src/db/api/organizations.js
Normal file
@ -0,0 +1,546 @@
|
||||
|
||||
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 OrganizationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const organizations = await db.organizations.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return organizations;
|
||||
}
|
||||
|
||||
|
||||
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 organizationsData = 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 organizations = await db.organizations.bulkCreate(organizationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return organizations;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const organizations = await db.organizations.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await organizations.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return organizations;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const organizations = await db.organizations.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of organizations) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of organizations) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return organizations;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const organizations = await db.organizations.findByPk(id, options);
|
||||
|
||||
await organizations.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await organizations.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return organizations;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const organizations = await db.organizations.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!organizations) {
|
||||
return organizations;
|
||||
}
|
||||
|
||||
const output = organizations.get({plain: true});
|
||||
|
||||
|
||||
output.users_organizations = await organizations.getUsers_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenants_organizations = await organizations.getTenants_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_lines_organizations = await organizations.getService_lines_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.services_organizations = await organizations.getServices_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.companies_organizations = await organizations.getCompanies_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contacts_organizations = await organizations.getContacts_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.tags_organizations = await organizations.getTags_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.pipelines_organizations = await organizations.getPipelines_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deal_stages_organizations = await organizations.getDeal_stages_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.leads_organizations = await organizations.getLeads_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deals_organizations = await organizations.getDeals_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_templates_organizations = await organizations.getProject_templates_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.projects_organizations = await organizations.getProjects_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.milestones_organizations = await organizations.getMilestones_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.tasks_organizations = await organizations.getTasks_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.task_dependencies_organizations = await organizations.getTask_dependencies_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deliverables_organizations = await organizations.getDeliverables_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_risks_organizations = await organizations.getProject_risks_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_status_reports_organizations = await organizations.getProject_status_reports_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.skills_organizations = await organizations.getSkills_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_members_organizations = await organizations.getTeam_members_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_member_skills_organizations = await organizations.getTeam_member_skills_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.resource_allocations_organizations = await organizations.getResource_allocations_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.leave_requests_organizations = await organizations.getLeave_requests_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.public_holidays_organizations = await organizations.getPublic_holidays_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.timesheets_organizations = await organizations.getTimesheets_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.time_entries_organizations = await organizations.getTime_entries_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.products_organizations = await organizations.getProducts_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contracts_organizations = await organizations.getContracts_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoices_organizations = await organizations.getInvoices_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoice_line_items_organizations = await organizations.getInvoice_line_items_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.payments_organizations = await organizations.getPayments_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.support_slas_organizations = await organizations.getSupport_slas_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.support_tickets_organizations = await organizations.getSupport_tickets_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.ticket_comments_organizations = await organizations.getTicket_comments_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.csat_surveys_organizations = await organizations.getCsat_surveys_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.email_templates_organizations = await organizations.getEmail_templates_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.notifications_organizations = await organizations.getNotifications_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.audit_logs_organizations = await organizations.getAudit_logs_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.imports_organizations = await organizations.getImports_organizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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(
|
||||
'organizations',
|
||||
'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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.organizations.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'organizations',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.organizations.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
655
backend/src/db/api/payments.js
Normal file
655
backend/src/db/api/payments.js
Normal file
@ -0,0 +1,655 @@
|
||||
|
||||
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 PaymentsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const payments = await db.payments.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
paid_at: data.paid_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: data.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: data.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider: data.provider
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider_reference: data.provider_reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await payments.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await payments.setInvoice( data.invoice || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await payments.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return payments;
|
||||
}
|
||||
|
||||
|
||||
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 paymentsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
paid_at: item.paid_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
amount: item.amount
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: item.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider: item.provider
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
provider_reference: item.provider_reference
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.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 payments = await db.payments.bulkCreate(paymentsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return payments;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const payments = await db.payments.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.paid_at !== undefined) updatePayload.paid_at = data.paid_at;
|
||||
|
||||
|
||||
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
||||
|
||||
|
||||
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
||||
|
||||
|
||||
if (data.provider !== undefined) updatePayload.provider = data.provider;
|
||||
|
||||
|
||||
if (data.provider_reference !== undefined) updatePayload.provider_reference = data.provider_reference;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await payments.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await payments.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.invoice !== undefined) {
|
||||
await payments.setInvoice(
|
||||
|
||||
data.invoice,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await payments.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return payments;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const payments = await db.payments.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of payments) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of payments) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return payments;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const payments = await db.payments.findByPk(id, options);
|
||||
|
||||
await payments.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await payments.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return payments;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const payments = await db.payments.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!payments) {
|
||||
return payments;
|
||||
}
|
||||
|
||||
const output = payments.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await payments.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoice = await payments.getInvoice({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await payments.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.invoices,
|
||||
as: 'invoice',
|
||||
|
||||
where: filter.invoice ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.invoice.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
invoice_number: {
|
||||
[Op.or]: filter.invoice.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.provider_reference) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'payments',
|
||||
'provider_reference',
|
||||
filter.provider_reference,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'payments',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.paid_atRange) {
|
||||
const [start, end] = filter.paid_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
paid_at: {
|
||||
...where.paid_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
paid_at: {
|
||||
...where.paid_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.amountRange) {
|
||||
const [start, end] = filter.amountRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
amount: {
|
||||
...where.amount,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.currency) {
|
||||
where = {
|
||||
...where,
|
||||
currency: filter.currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.provider) {
|
||||
where = {
|
||||
...where,
|
||||
provider: filter.provider,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.payments.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'payments',
|
||||
'provider_reference',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.payments.findAll({
|
||||
attributes: [ 'id', 'provider_reference' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['provider_reference', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.provider_reference,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
372
backend/src/db/api/permissions.js
Normal file
372
backend/src/db/api/permissions.js
Normal file
@ -0,0 +1,372 @@
|
||||
|
||||
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 globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
547
backend/src/db/api/pipelines.js
Normal file
547
backend/src/db/api/pipelines.js
Normal file
@ -0,0 +1,547 @@
|
||||
|
||||
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 PipelinesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const pipelines = await db.pipelines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pipeline_type: data.pipeline_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
sort_order: data.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await pipelines.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await pipelines.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
|
||||
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 pipelinesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pipeline_type: item.pipeline_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
sort_order: item.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const pipelines = await db.pipelines.bulkCreate(pipelinesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const pipelines = await db.pipelines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.pipeline_type !== undefined) updatePayload.pipeline_type = data.pipeline_type;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await pipelines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await pipelines.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await pipelines.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const pipelines = await db.pipelines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of pipelines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of pipelines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const pipelines = await db.pipelines.findByPk(id, options);
|
||||
|
||||
await pipelines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await pipelines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const pipelines = await db.pipelines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!pipelines) {
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
const output = pipelines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.deal_stages_pipeline = await pipelines.getDeal_stages_pipeline({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.deals_pipeline = await pipelines.getDeals_pipeline({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await pipelines.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await pipelines.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'pipelines',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.sort_orderRange) {
|
||||
const [start, end] = filter.sort_orderRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.pipeline_type) {
|
||||
where = {
|
||||
...where,
|
||||
pipeline_type: filter.pipeline_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.pipelines.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'pipelines',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.pipelines.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
633
backend/src/db/api/products.js
Normal file
633
backend/src/db/api/products.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 ProductsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const products = await db.products.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sku: data.sku
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_price: data.unit_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: data.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit: data.unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
taxable: data.taxable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await products.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await products.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
|
||||
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 productsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sku: item.sku
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit_price: item.unit_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
currency: item.currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
unit: item.unit
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
taxable: item.taxable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
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 products = await db.products.bulkCreate(productsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const products = await db.products.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.sku !== undefined) updatePayload.sku = data.sku;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.unit_price !== undefined) updatePayload.unit_price = data.unit_price;
|
||||
|
||||
|
||||
if (data.currency !== undefined) updatePayload.currency = data.currency;
|
||||
|
||||
|
||||
if (data.unit !== undefined) updatePayload.unit = data.unit;
|
||||
|
||||
|
||||
if (data.taxable !== undefined) updatePayload.taxable = data.taxable;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await products.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await products.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await products.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const products = await db.products.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of products) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of products) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const products = await db.products.findByPk(id, options);
|
||||
|
||||
await products.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await products.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const products = await db.products.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!products) {
|
||||
return products;
|
||||
}
|
||||
|
||||
const output = products.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.invoice_line_items_product = await products.getInvoice_line_items_product({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await products.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await products.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'products',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.sku) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'products',
|
||||
'sku',
|
||||
filter.sku,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'products',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.unit_priceRange) {
|
||||
const [start, end] = filter.unit_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_price: {
|
||||
...where.unit_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
unit_price: {
|
||||
...where.unit_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.currency) {
|
||||
where = {
|
||||
...where,
|
||||
currency: filter.currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.unit) {
|
||||
where = {
|
||||
...where,
|
||||
unit: filter.unit,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.taxable) {
|
||||
where = {
|
||||
...where,
|
||||
taxable: filter.taxable,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.products.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'products',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.products.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
642
backend/src/db/api/project_risks.js
Normal file
642
backend/src/db/api/project_risks.js
Normal file
@ -0,0 +1,642 @@
|
||||
|
||||
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 Project_risksDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_risks = await db.project_risks.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
title: data.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
impact: data.impact
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
likelihood: data.likelihood
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
mitigation_plan: data.mitigation_plan
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await project_risks.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_risks.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_risks.setOwner( data.owner || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_risks.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
|
||||
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 project_risksData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
title: item.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
impact: item.impact
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
likelihood: item.likelihood
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
mitigation_plan: item.mitigation_plan
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const project_risks = await db.project_risks.bulkCreate(project_risksData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const project_risks = await db.project_risks.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.title !== undefined) updatePayload.title = data.title;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.impact !== undefined) updatePayload.impact = data.impact;
|
||||
|
||||
|
||||
if (data.likelihood !== undefined) updatePayload.likelihood = data.likelihood;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.mitigation_plan !== undefined) updatePayload.mitigation_plan = data.mitigation_plan;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await project_risks.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await project_risks.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await project_risks.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.owner !== undefined) {
|
||||
await project_risks.setOwner(
|
||||
|
||||
data.owner,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await project_risks.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_risks = await db.project_risks.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of project_risks) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of project_risks) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_risks = await db.project_risks.findByPk(id, options);
|
||||
|
||||
await project_risks.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_risks.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_risks = await db.project_risks.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!project_risks) {
|
||||
return project_risks;
|
||||
}
|
||||
|
||||
const output = project_risks.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await project_risks.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await project_risks.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.owner = await project_risks.getOwner({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await project_risks.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.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.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.title) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_risks',
|
||||
'title',
|
||||
filter.title,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_risks',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.mitigation_plan) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_risks',
|
||||
'mitigation_plan',
|
||||
filter.mitigation_plan,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.impact) {
|
||||
where = {
|
||||
...where,
|
||||
impact: filter.impact,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.likelihood) {
|
||||
where = {
|
||||
...where,
|
||||
likelihood: filter.likelihood,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
where = {
|
||||
...where,
|
||||
status: filter.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.project_risks.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'project_risks',
|
||||
'title',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.project_risks.findAll({
|
||||
attributes: [ 'id', 'title' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['title', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.title,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
700
backend/src/db/api/project_status_reports.js
Normal file
700
backend/src/db/api/project_status_reports.js
Normal file
@ -0,0 +1,700 @@
|
||||
|
||||
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 Project_status_reportsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_status_reports = await db.project_status_reports.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
reporting_week_start_at: data.reporting_week_start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reporting_week_end_at: data.reporting_week_end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
overall_status: data.overall_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
accomplishments: data.accomplishments
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
next_steps: data.next_steps
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
risks_issues: data.risks_issues
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
client_notes: data.client_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await project_status_reports.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_status_reports.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_status_reports.setAuthor( data.author || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_status_reports.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
|
||||
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 project_status_reportsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
reporting_week_start_at: item.reporting_week_start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
reporting_week_end_at: item.reporting_week_end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
overall_status: item.overall_status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
accomplishments: item.accomplishments
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
next_steps: item.next_steps
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
risks_issues: item.risks_issues
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
client_notes: item.client_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const project_status_reports = await db.project_status_reports.bulkCreate(project_status_reportsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const project_status_reports = await db.project_status_reports.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.reporting_week_start_at !== undefined) updatePayload.reporting_week_start_at = data.reporting_week_start_at;
|
||||
|
||||
|
||||
if (data.reporting_week_end_at !== undefined) updatePayload.reporting_week_end_at = data.reporting_week_end_at;
|
||||
|
||||
|
||||
if (data.overall_status !== undefined) updatePayload.overall_status = data.overall_status;
|
||||
|
||||
|
||||
if (data.accomplishments !== undefined) updatePayload.accomplishments = data.accomplishments;
|
||||
|
||||
|
||||
if (data.next_steps !== undefined) updatePayload.next_steps = data.next_steps;
|
||||
|
||||
|
||||
if (data.risks_issues !== undefined) updatePayload.risks_issues = data.risks_issues;
|
||||
|
||||
|
||||
if (data.client_notes !== undefined) updatePayload.client_notes = data.client_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await project_status_reports.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await project_status_reports.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await project_status_reports.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.author !== undefined) {
|
||||
await project_status_reports.setAuthor(
|
||||
|
||||
data.author,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await project_status_reports.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_status_reports = await db.project_status_reports.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of project_status_reports) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of project_status_reports) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_status_reports = await db.project_status_reports.findByPk(id, options);
|
||||
|
||||
await project_status_reports.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_status_reports.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_status_reports = await db.project_status_reports.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!project_status_reports) {
|
||||
return project_status_reports;
|
||||
}
|
||||
|
||||
const output = project_status_reports.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await project_status_reports.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await project_status_reports.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.author = await project_status_reports.getAuthor({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await project_status_reports.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'author',
|
||||
|
||||
where: filter.author ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.author.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.author.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.accomplishments) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_status_reports',
|
||||
'accomplishments',
|
||||
filter.accomplishments,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.next_steps) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_status_reports',
|
||||
'next_steps',
|
||||
filter.next_steps,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.risks_issues) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_status_reports',
|
||||
'risks_issues',
|
||||
filter.risks_issues,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.client_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_status_reports',
|
||||
'client_notes',
|
||||
filter.client_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.reporting_week_start_atRange) {
|
||||
const [start, end] = filter.reporting_week_start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reporting_week_start_at: {
|
||||
...where.reporting_week_start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reporting_week_start_at: {
|
||||
...where.reporting_week_start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.reporting_week_end_atRange) {
|
||||
const [start, end] = filter.reporting_week_end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reporting_week_end_at: {
|
||||
...where.reporting_week_end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
reporting_week_end_at: {
|
||||
...where.reporting_week_end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.overall_status) {
|
||||
where = {
|
||||
...where,
|
||||
overall_status: filter.overall_status,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.project_status_reports.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'project_status_reports',
|
||||
'overall_status',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.project_status_reports.findAll({
|
||||
attributes: [ 'id', 'overall_status' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['overall_status', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.overall_status,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
641
backend/src/db/api/project_templates.js
Normal file
641
backend/src/db/api/project_templates.js
Normal file
@ -0,0 +1,641 @@
|
||||
|
||||
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 Project_templatesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_templates = await db.project_templates.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_duration_weeks: data.default_duration_weeks
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_budget: data.default_budget
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_model: data.billing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await project_templates.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_templates.setService_line( data.service_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_templates.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
|
||||
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 project_templatesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_duration_weeks: item.default_duration_weeks
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_budget: item.default_budget
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_model: item.billing_model
|
||||
||
|
||||
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 project_templates = await db.project_templates.bulkCreate(project_templatesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const project_templates = await db.project_templates.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.default_duration_weeks !== undefined) updatePayload.default_duration_weeks = data.default_duration_weeks;
|
||||
|
||||
|
||||
if (data.default_budget !== undefined) updatePayload.default_budget = data.default_budget;
|
||||
|
||||
|
||||
if (data.billing_model !== undefined) updatePayload.billing_model = data.billing_model;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await project_templates.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await project_templates.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_line !== undefined) {
|
||||
await project_templates.setService_line(
|
||||
|
||||
data.service_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await project_templates.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_templates = await db.project_templates.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of project_templates) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of project_templates) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_templates = await db.project_templates.findByPk(id, options);
|
||||
|
||||
await project_templates.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await project_templates.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const project_templates = await db.project_templates.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!project_templates) {
|
||||
return project_templates;
|
||||
}
|
||||
|
||||
const output = project_templates.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.projects_template = await project_templates.getProjects_template({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await project_templates.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_line = await project_templates.getService_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await project_templates.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.service_lines,
|
||||
as: 'service_line',
|
||||
|
||||
where: filter.service_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_templates',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'project_templates',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.default_duration_weeksRange) {
|
||||
const [start, end] = filter.default_duration_weeksRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_duration_weeks: {
|
||||
...where.default_duration_weeks,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_duration_weeks: {
|
||||
...where.default_duration_weeks,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.default_budgetRange) {
|
||||
const [start, end] = filter.default_budgetRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_budget: {
|
||||
...where.default_budget,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
default_budget: {
|
||||
...where.default_budget,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.billing_model) {
|
||||
where = {
|
||||
...where,
|
||||
billing_model: filter.billing_model,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.project_templates.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'project_templates',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.project_templates.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
1049
backend/src/db/api/projects.js
Normal file
1049
backend/src/db/api/projects.js
Normal file
File diff suppressed because it is too large
Load Diff
598
backend/src/db/api/public_holidays.js
Normal file
598
backend/src/db/api/public_holidays.js
Normal file
@ -0,0 +1,598 @@
|
||||
|
||||
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 Public_holidaysDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const public_holidays = await db.public_holidays.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: data.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
country: data.country
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
is_recurring_yearly: data.is_recurring_yearly
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await public_holidays.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await public_holidays.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
|
||||
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 public_holidaysData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: item.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
country: item.country
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
is_recurring_yearly: item.is_recurring_yearly
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const public_holidays = await db.public_holidays.bulkCreate(public_holidaysData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const public_holidays = await db.public_holidays.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
|
||||
|
||||
|
||||
if (data.country !== undefined) updatePayload.country = data.country;
|
||||
|
||||
|
||||
if (data.is_recurring_yearly !== undefined) updatePayload.is_recurring_yearly = data.is_recurring_yearly;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await public_holidays.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await public_holidays.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await public_holidays.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const public_holidays = await db.public_holidays.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of public_holidays) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of public_holidays) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const public_holidays = await db.public_holidays.findByPk(id, options);
|
||||
|
||||
await public_holidays.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await public_holidays.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const public_holidays = await db.public_holidays.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!public_holidays) {
|
||||
return public_holidays;
|
||||
}
|
||||
|
||||
const output = public_holidays.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await public_holidays.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await public_holidays.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'public_holidays',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.country) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'public_holidays',
|
||||
'country',
|
||||
filter.country,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
start_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
end_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.end_atRange) {
|
||||
const [start, end] = filter.end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.is_recurring_yearly) {
|
||||
where = {
|
||||
...where,
|
||||
is_recurring_yearly: filter.is_recurring_yearly,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.public_holidays.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'public_holidays',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.public_holidays.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
683
backend/src/db/api/resource_allocations.js
Normal file
683
backend/src/db/api/resource_allocations.js
Normal file
@ -0,0 +1,683 @@
|
||||
|
||||
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 Resource_allocationsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const resource_allocations = await db.resource_allocations.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: data.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allocated_hours: data.allocated_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allocation_type: data.allocation_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await resource_allocations.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await resource_allocations.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await resource_allocations.setTeam_member( data.team_member || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await resource_allocations.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
|
||||
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 resource_allocationsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
end_at: item.end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allocated_hours: item.allocated_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
allocation_type: item.allocation_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 resource_allocations = await db.resource_allocations.bulkCreate(resource_allocationsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const resource_allocations = await db.resource_allocations.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
|
||||
|
||||
|
||||
if (data.allocated_hours !== undefined) updatePayload.allocated_hours = data.allocated_hours;
|
||||
|
||||
|
||||
if (data.allocation_type !== undefined) updatePayload.allocation_type = data.allocation_type;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await resource_allocations.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await resource_allocations.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await resource_allocations.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.team_member !== undefined) {
|
||||
await resource_allocations.setTeam_member(
|
||||
|
||||
data.team_member,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await resource_allocations.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const resource_allocations = await db.resource_allocations.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of resource_allocations) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of resource_allocations) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const resource_allocations = await db.resource_allocations.findByPk(id, options);
|
||||
|
||||
await resource_allocations.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await resource_allocations.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const resource_allocations = await db.resource_allocations.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!resource_allocations) {
|
||||
return resource_allocations;
|
||||
}
|
||||
|
||||
const output = resource_allocations.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await resource_allocations.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await resource_allocations.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_member = await resource_allocations.getTeam_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await resource_allocations.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.team_members,
|
||||
as: 'team_member',
|
||||
|
||||
where: filter.team_member ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.team_member.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
notes: {
|
||||
[Op.or]: filter.team_member.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'resource_allocations',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.calendarStart && filter.calendarEnd) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.or]: [
|
||||
{
|
||||
start_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
{
|
||||
end_at: {
|
||||
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.end_atRange) {
|
||||
const [start, end] = filter.end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
end_at: {
|
||||
...where.end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.allocated_hoursRange) {
|
||||
const [start, end] = filter.allocated_hoursRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
allocated_hours: {
|
||||
...where.allocated_hours,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
allocated_hours: {
|
||||
...where.allocated_hours,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.allocation_type) {
|
||||
where = {
|
||||
...where,
|
||||
allocation_type: filter.allocation_type,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.resource_allocations.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'resource_allocations',
|
||||
'notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.resource_allocations.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
474
backend/src/db/api/roles.js
Normal file
474
backend/src/db/api/roles.js
Normal file
@ -0,0 +1,474 @@
|
||||
|
||||
const db = require('../models');
|
||||
const FileDBApi = require('./file');
|
||||
const crypto = require('crypto');
|
||||
const Utils = require('../utils');
|
||||
|
||||
|
||||
const config = require('../../config');
|
||||
|
||||
|
||||
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
|
||||
,
|
||||
|
||||
globalAccess: data.globalAccess
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
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
|
||||
,
|
||||
|
||||
globalAccess: item.globalAccess
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
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 globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
if (data.globalAccess !== undefined) updatePayload.globalAccess = data.globalAccess;
|
||||
|
||||
|
||||
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,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
|
||||
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.globalAccess) {
|
||||
where = {
|
||||
...where,
|
||||
globalAccess: filter.globalAccess,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!globalAccess) {
|
||||
where = { name: { [Op.ne]: config.roles.super_admin } };
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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, globalAccess,) {
|
||||
let where = {};
|
||||
|
||||
if (!globalAccess) {
|
||||
where = { name: { [Op.ne]: config.roles.super_admin } };
|
||||
}
|
||||
|
||||
|
||||
|
||||
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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
720
backend/src/db/api/service_lines.js
Normal file
720
backend/src/db/api/service_lines.js
Normal file
@ -0,0 +1,720 @@
|
||||
|
||||
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 Service_linesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const service_lines = await db.service_lines.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name_fr: data.name_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
name_en: data.name_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_fr: data.description_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_en: data.description_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
category: data.category
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pricing_model: data.pricing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
min_price: data.min_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
max_price: data.max_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
typical_timeline: data.typical_timeline
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await service_lines.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await service_lines.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return service_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 service_linesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name_fr: item.name_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
name_en: item.name_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_fr: item.description_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_en: item.description_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
category: item.category
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pricing_model: item.pricing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
min_price: item.min_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
max_price: item.max_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
typical_timeline: item.typical_timeline
|
||||
||
|
||||
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 service_lines = await db.service_lines.bulkCreate(service_linesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return service_lines;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const service_lines = await db.service_lines.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name_fr !== undefined) updatePayload.name_fr = data.name_fr;
|
||||
|
||||
|
||||
if (data.name_en !== undefined) updatePayload.name_en = data.name_en;
|
||||
|
||||
|
||||
if (data.description_fr !== undefined) updatePayload.description_fr = data.description_fr;
|
||||
|
||||
|
||||
if (data.description_en !== undefined) updatePayload.description_en = data.description_en;
|
||||
|
||||
|
||||
if (data.category !== undefined) updatePayload.category = data.category;
|
||||
|
||||
|
||||
if (data.pricing_model !== undefined) updatePayload.pricing_model = data.pricing_model;
|
||||
|
||||
|
||||
if (data.min_price !== undefined) updatePayload.min_price = data.min_price;
|
||||
|
||||
|
||||
if (data.max_price !== undefined) updatePayload.max_price = data.max_price;
|
||||
|
||||
|
||||
if (data.typical_timeline !== undefined) updatePayload.typical_timeline = data.typical_timeline;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await service_lines.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await service_lines.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await service_lines.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return service_lines;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const service_lines = await db.service_lines.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of service_lines) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of service_lines) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return service_lines;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const service_lines = await db.service_lines.findByPk(id, options);
|
||||
|
||||
await service_lines.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await service_lines.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return service_lines;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const service_lines = await db.service_lines.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!service_lines) {
|
||||
return service_lines;
|
||||
}
|
||||
|
||||
const output = service_lines.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.services_service_line = await service_lines.getServices_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.deals_service_line = await service_lines.getDeals_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_templates_service_line = await service_lines.getProject_templates_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.projects_service_line = await service_lines.getProjects_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.time_entries_service_line = await service_lines.getTime_entries_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.contracts_service_line = await service_lines.getContracts_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.support_tickets_service_line = await service_lines.getSupport_tickets_service_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await service_lines.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await service_lines.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'service_lines',
|
||||
'name_fr',
|
||||
filter.name_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.name_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'service_lines',
|
||||
'name_en',
|
||||
filter.name_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'service_lines',
|
||||
'description_fr',
|
||||
filter.description_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'service_lines',
|
||||
'description_en',
|
||||
filter.description_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.typical_timeline) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'service_lines',
|
||||
'typical_timeline',
|
||||
filter.typical_timeline,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.min_priceRange) {
|
||||
const [start, end] = filter.min_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
min_price: {
|
||||
...where.min_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
min_price: {
|
||||
...where.min_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.max_priceRange) {
|
||||
const [start, end] = filter.max_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
max_price: {
|
||||
...where.max_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
max_price: {
|
||||
...where.max_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.category) {
|
||||
where = {
|
||||
...where,
|
||||
category: filter.category,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.pricing_model) {
|
||||
where = {
|
||||
...where,
|
||||
pricing_model: filter.pricing_model,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.service_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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'service_lines',
|
||||
'name_fr',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.service_lines.findAll({
|
||||
attributes: [ 'id', 'name_fr' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['name_fr', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.name_fr,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
774
backend/src/db/api/services.js
Normal file
774
backend/src/db/api/services.js
Normal file
@ -0,0 +1,774 @@
|
||||
|
||||
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 ServicesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const services = await db.services.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name_fr: data.name_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
name_en: data.name_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_fr: data.description_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_en: data.description_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
deliverables: data.deliverables
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pricing_model: data.pricing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
min_price: data.min_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
max_price: data.max_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
typical_duration_weeks: data.typical_duration_weeks
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
required_skills: data.required_skills
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await services.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await services.setService_line( data.service_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await services.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
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 servicesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name_fr: item.name_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
name_en: item.name_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_fr: item.description_fr
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description_en: item.description_en
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
deliverables: item.deliverables
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
pricing_model: item.pricing_model
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
min_price: item.min_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
max_price: item.max_price
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
typical_duration_weeks: item.typical_duration_weeks
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
required_skills: item.required_skills
|
||||
||
|
||||
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 services = await db.services.bulkCreate(servicesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const services = await db.services.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name_fr !== undefined) updatePayload.name_fr = data.name_fr;
|
||||
|
||||
|
||||
if (data.name_en !== undefined) updatePayload.name_en = data.name_en;
|
||||
|
||||
|
||||
if (data.description_fr !== undefined) updatePayload.description_fr = data.description_fr;
|
||||
|
||||
|
||||
if (data.description_en !== undefined) updatePayload.description_en = data.description_en;
|
||||
|
||||
|
||||
if (data.deliverables !== undefined) updatePayload.deliverables = data.deliverables;
|
||||
|
||||
|
||||
if (data.pricing_model !== undefined) updatePayload.pricing_model = data.pricing_model;
|
||||
|
||||
|
||||
if (data.min_price !== undefined) updatePayload.min_price = data.min_price;
|
||||
|
||||
|
||||
if (data.max_price !== undefined) updatePayload.max_price = data.max_price;
|
||||
|
||||
|
||||
if (data.typical_duration_weeks !== undefined) updatePayload.typical_duration_weeks = data.typical_duration_weeks;
|
||||
|
||||
|
||||
if (data.required_skills !== undefined) updatePayload.required_skills = data.required_skills;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await services.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await services.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_line !== undefined) {
|
||||
await services.setService_line(
|
||||
|
||||
data.service_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await services.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const services = await db.services.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of services) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of services) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const services = await db.services.findByPk(id, options);
|
||||
|
||||
await services.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await services.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const services = await db.services.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!services) {
|
||||
return services;
|
||||
}
|
||||
|
||||
const output = services.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.leads_service_interest = await services.getLeads_service_interest({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await services.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_line = await services.getService_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await services.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.service_lines,
|
||||
as: 'service_line',
|
||||
|
||||
where: filter.service_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'name_fr',
|
||||
filter.name_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.name_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'name_en',
|
||||
filter.name_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description_fr) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'description_fr',
|
||||
filter.description_fr,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description_en) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'description_en',
|
||||
filter.description_en,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.deliverables) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'deliverables',
|
||||
filter.deliverables,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.required_skills) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'services',
|
||||
'required_skills',
|
||||
filter.required_skills,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.min_priceRange) {
|
||||
const [start, end] = filter.min_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
min_price: {
|
||||
...where.min_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
min_price: {
|
||||
...where.min_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.max_priceRange) {
|
||||
const [start, end] = filter.max_priceRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
max_price: {
|
||||
...where.max_price,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
max_price: {
|
||||
...where.max_price,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.typical_duration_weeksRange) {
|
||||
const [start, end] = filter.typical_duration_weeksRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
typical_duration_weeks: {
|
||||
...where.typical_duration_weeks,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
typical_duration_weeks: {
|
||||
...where.typical_duration_weeks,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.pricing_model) {
|
||||
where = {
|
||||
...where,
|
||||
pricing_model: filter.pricing_model,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.services.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'services',
|
||||
'name_fr',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.services.findAll({
|
||||
attributes: [ 'id', 'name_fr' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['name_fr', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.name_fr,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
484
backend/src/db/api/skills.js
Normal file
484
backend/src/db/api/skills.js
Normal file
@ -0,0 +1,484 @@
|
||||
|
||||
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 SkillsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const skills = await db.skills.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
domain: data.domain
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await skills.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await skills.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
|
||||
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 skillsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
domain: item.domain
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const skills = await db.skills.bulkCreate(skillsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const skills = await db.skills.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.domain !== undefined) updatePayload.domain = data.domain;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await skills.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await skills.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await skills.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const skills = await db.skills.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of skills) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of skills) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const skills = await db.skills.findByPk(id, options);
|
||||
|
||||
await skills.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await skills.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const skills = await db.skills.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!skills) {
|
||||
return skills;
|
||||
}
|
||||
|
||||
const output = skills.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.team_member_skills_skill = await skills.getTeam_member_skills_skill({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await skills.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await skills.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'skills',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.domain) {
|
||||
where = {
|
||||
...where,
|
||||
domain: filter.domain,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.skills.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'skills',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.skills.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
580
backend/src/db/api/support_slas.js
Normal file
580
backend/src/db/api/support_slas.js
Normal file
@ -0,0 +1,580 @@
|
||||
|
||||
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 Support_slasDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const support_slas = await db.support_slas.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
priority: data.priority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
first_response_minutes: data.first_response_minutes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
resolution_minutes: data.resolution_minutes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await support_slas.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await support_slas.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
|
||||
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 support_slasData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
priority: item.priority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
first_response_minutes: item.first_response_minutes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
resolution_minutes: item.resolution_minutes
|
||||
||
|
||||
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 support_slas = await db.support_slas.bulkCreate(support_slasData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const support_slas = await db.support_slas.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.priority !== undefined) updatePayload.priority = data.priority;
|
||||
|
||||
|
||||
if (data.first_response_minutes !== undefined) updatePayload.first_response_minutes = data.first_response_minutes;
|
||||
|
||||
|
||||
if (data.resolution_minutes !== undefined) updatePayload.resolution_minutes = data.resolution_minutes;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await support_slas.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await support_slas.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await support_slas.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const support_slas = await db.support_slas.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of support_slas) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of support_slas) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const support_slas = await db.support_slas.findByPk(id, options);
|
||||
|
||||
await support_slas.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await support_slas.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const support_slas = await db.support_slas.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!support_slas) {
|
||||
return support_slas;
|
||||
}
|
||||
|
||||
const output = support_slas.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.support_tickets_sla = await support_slas.getSupport_tickets_sla({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await support_slas.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await support_slas.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'support_slas',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.first_response_minutesRange) {
|
||||
const [start, end] = filter.first_response_minutesRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
first_response_minutes: {
|
||||
...where.first_response_minutes,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
first_response_minutes: {
|
||||
...where.first_response_minutes,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.resolution_minutesRange) {
|
||||
const [start, end] = filter.resolution_minutesRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
resolution_minutes: {
|
||||
...where.resolution_minutes,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
resolution_minutes: {
|
||||
...where.resolution_minutes,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.priority) {
|
||||
where = {
|
||||
...where,
|
||||
priority: filter.priority,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.support_slas.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'support_slas',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.support_slas.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
1084
backend/src/db/api/support_tickets.js
Normal file
1084
backend/src/db/api/support_tickets.js
Normal file
File diff suppressed because it is too large
Load Diff
484
backend/src/db/api/tags.js
Normal file
484
backend/src/db/api/tags.js
Normal file
@ -0,0 +1,484 @@
|
||||
|
||||
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 TagsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tags = await db.tags.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
color: data.color
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await tags.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tags.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
|
||||
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 tagsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
color: item.color
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const tags = await db.tags.bulkCreate(tagsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const tags = await db.tags.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.color !== undefined) updatePayload.color = data.color;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await tags.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await tags.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await tags.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tags = await db.tags.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of tags) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of tags) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tags = await db.tags.findByPk(id, options);
|
||||
|
||||
await tags.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tags.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tags = await db.tags.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!tags) {
|
||||
return tags;
|
||||
}
|
||||
|
||||
const output = tags.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await tags.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await tags.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tags',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.color) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tags',
|
||||
'color',
|
||||
filter.color,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.tags.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'tags',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.tags.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
510
backend/src/db/api/task_dependencies.js
Normal file
510
backend/src/db/api/task_dependencies.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 Task_dependenciesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const task_dependencies = await db.task_dependencies.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await task_dependencies.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await task_dependencies.setTask( data.task || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await task_dependencies.setDepends_on_task( data.depends_on_task || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await task_dependencies.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
|
||||
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 task_dependenciesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const task_dependencies = await db.task_dependencies.bulkCreate(task_dependenciesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const task_dependencies = await db.task_dependencies.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await task_dependencies.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await task_dependencies.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.task !== undefined) {
|
||||
await task_dependencies.setTask(
|
||||
|
||||
data.task,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.depends_on_task !== undefined) {
|
||||
await task_dependencies.setDepends_on_task(
|
||||
|
||||
data.depends_on_task,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await task_dependencies.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const task_dependencies = await db.task_dependencies.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of task_dependencies) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of task_dependencies) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const task_dependencies = await db.task_dependencies.findByPk(id, options);
|
||||
|
||||
await task_dependencies.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await task_dependencies.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const task_dependencies = await db.task_dependencies.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!task_dependencies) {
|
||||
return task_dependencies;
|
||||
}
|
||||
|
||||
const output = task_dependencies.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await task_dependencies.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.task = await task_dependencies.getTask({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.depends_on_task = await task_dependencies.getDepends_on_task({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await task_dependencies.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.tasks,
|
||||
as: 'task',
|
||||
|
||||
where: filter.task ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.task.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
title: {
|
||||
[Op.or]: filter.task.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.tasks,
|
||||
as: 'depends_on_task',
|
||||
|
||||
where: filter.depends_on_task ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.depends_on_task.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
title: {
|
||||
[Op.or]: filter.depends_on_task.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.task_dependencies.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'task_dependencies',
|
||||
'task',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.task_dependencies.findAll({
|
||||
attributes: [ 'id', 'task' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['task', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.task,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
832
backend/src/db/api/tasks.js
Normal file
832
backend/src/db/api/tasks.js
Normal file
@ -0,0 +1,832 @@
|
||||
|
||||
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 TasksDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tasks = await db.tasks.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
title: data.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
priority: data.priority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: data.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: data.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
estimated_hours: data.estimated_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_hours: data.actual_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: data.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await tasks.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tasks.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tasks.setMilestone( data.milestone || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tasks.setAssignee( data.assignee || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tasks.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
|
||||
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 tasksData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
title: item.title
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
priority: item.priority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
start_at: item.start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
due_at: item.due_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
estimated_hours: item.estimated_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
actual_hours: item.actual_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
sort_order: item.sort_order
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const tasks = await db.tasks.bulkCreate(tasksData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const tasks = await db.tasks.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.title !== undefined) updatePayload.title = data.title;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.priority !== undefined) updatePayload.priority = data.priority;
|
||||
|
||||
|
||||
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
|
||||
|
||||
|
||||
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
|
||||
|
||||
|
||||
if (data.estimated_hours !== undefined) updatePayload.estimated_hours = data.estimated_hours;
|
||||
|
||||
|
||||
if (data.actual_hours !== undefined) updatePayload.actual_hours = data.actual_hours;
|
||||
|
||||
|
||||
if (data.sort_order !== undefined) updatePayload.sort_order = data.sort_order;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await tasks.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await tasks.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await tasks.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.milestone !== undefined) {
|
||||
await tasks.setMilestone(
|
||||
|
||||
data.milestone,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.assignee !== undefined) {
|
||||
await tasks.setAssignee(
|
||||
|
||||
data.assignee,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await tasks.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tasks = await db.tasks.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of tasks) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of tasks) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tasks = await db.tasks.findByPk(id, options);
|
||||
|
||||
await tasks.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tasks.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tasks = await db.tasks.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!tasks) {
|
||||
return tasks;
|
||||
}
|
||||
|
||||
const output = tasks.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.task_dependencies_task = await tasks.getTask_dependencies_task({
|
||||
transaction
|
||||
});
|
||||
|
||||
output.task_dependencies_depends_on_task = await tasks.getTask_dependencies_depends_on_task({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.time_entries_task = await tasks.getTime_entries_task({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await tasks.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await tasks.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.milestone = await tasks.getMilestone({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.assignee = await tasks.getAssignee({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await tasks.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.milestones,
|
||||
as: 'milestone',
|
||||
|
||||
where: filter.milestone ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.milestone.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.milestone.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'assignee',
|
||||
|
||||
where: filter.assignee ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.assignee.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.assignee.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.title) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tasks',
|
||||
'title',
|
||||
filter.title,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tasks',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.start_atRange) {
|
||||
const [start, end] = filter.start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
start_at: {
|
||||
...where.start_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.estimated_hoursRange) {
|
||||
const [start, end] = filter.estimated_hoursRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
estimated_hours: {
|
||||
...where.estimated_hours,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
estimated_hours: {
|
||||
...where.estimated_hours,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.actual_hoursRange) {
|
||||
const [start, end] = filter.actual_hoursRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_hours: {
|
||||
...where.actual_hours,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
actual_hours: {
|
||||
...where.actual_hours,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.sort_orderRange) {
|
||||
const [start, end] = filter.sort_orderRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
sort_order: {
|
||||
...where.sort_order,
|
||||
[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.priority) {
|
||||
where = {
|
||||
...where,
|
||||
priority: filter.priority,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.tasks.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'tasks',
|
||||
'title',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.tasks.findAll({
|
||||
attributes: [ 'id', 'title' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['title', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.title,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
530
backend/src/db/api/team_member_skills.js
Normal file
530
backend/src/db/api/team_member_skills.js
Normal file
@ -0,0 +1,530 @@
|
||||
|
||||
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 Team_member_skillsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_member_skills = await db.team_member_skills.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
proficiency: data.proficiency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await team_member_skills.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_member_skills.setTeam_member( data.team_member || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_member_skills.setSkill( data.skill || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_member_skills.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
|
||||
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 team_member_skillsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
proficiency: item.proficiency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const team_member_skills = await db.team_member_skills.bulkCreate(team_member_skillsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const team_member_skills = await db.team_member_skills.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.proficiency !== undefined) updatePayload.proficiency = data.proficiency;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await team_member_skills.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await team_member_skills.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.team_member !== undefined) {
|
||||
await team_member_skills.setTeam_member(
|
||||
|
||||
data.team_member,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.skill !== undefined) {
|
||||
await team_member_skills.setSkill(
|
||||
|
||||
data.skill,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await team_member_skills.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_member_skills = await db.team_member_skills.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of team_member_skills) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of team_member_skills) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_member_skills = await db.team_member_skills.findByPk(id, options);
|
||||
|
||||
await team_member_skills.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_member_skills.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_member_skills = await db.team_member_skills.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!team_member_skills) {
|
||||
return team_member_skills;
|
||||
}
|
||||
|
||||
const output = team_member_skills.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await team_member_skills.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_member = await team_member_skills.getTeam_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.skill = await team_member_skills.getSkill({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await team_member_skills.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.team_members,
|
||||
as: 'team_member',
|
||||
|
||||
where: filter.team_member ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.team_member.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
notes: {
|
||||
[Op.or]: filter.team_member.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.skills,
|
||||
as: 'skill',
|
||||
|
||||
where: filter.skill ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.skill.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.skill.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.proficiency) {
|
||||
where = {
|
||||
...where,
|
||||
proficiency: filter.proficiency,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.team_member_skills.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'team_member_skills',
|
||||
'proficiency',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.team_member_skills.findAll({
|
||||
attributes: [ 'id', 'proficiency' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['proficiency', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.proficiency,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
665
backend/src/db/api/team_members.js
Normal file
665
backend/src/db/api/team_members.js
Normal file
@ -0,0 +1,665 @@
|
||||
|
||||
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 Team_membersDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_members = await db.team_members.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
employment_type: data.employment_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
seniority: data.seniority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
hourly_rate: data.hourly_rate
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
rate_currency: data.rate_currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
weekly_capacity_hours: data.weekly_capacity_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: data.active
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
notes: data.notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await team_members.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_members.setUser( data.user || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_members.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return team_members;
|
||||
}
|
||||
|
||||
|
||||
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 team_membersData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
employment_type: item.employment_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
seniority: item.seniority
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
hourly_rate: item.hourly_rate
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
rate_currency: item.rate_currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
weekly_capacity_hours: item.weekly_capacity_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
active: item.active
|
||||
||
|
||||
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 team_members = await db.team_members.bulkCreate(team_membersData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return team_members;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const team_members = await db.team_members.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.employment_type !== undefined) updatePayload.employment_type = data.employment_type;
|
||||
|
||||
|
||||
if (data.seniority !== undefined) updatePayload.seniority = data.seniority;
|
||||
|
||||
|
||||
if (data.hourly_rate !== undefined) updatePayload.hourly_rate = data.hourly_rate;
|
||||
|
||||
|
||||
if (data.rate_currency !== undefined) updatePayload.rate_currency = data.rate_currency;
|
||||
|
||||
|
||||
if (data.weekly_capacity_hours !== undefined) updatePayload.weekly_capacity_hours = data.weekly_capacity_hours;
|
||||
|
||||
|
||||
if (data.active !== undefined) updatePayload.active = data.active;
|
||||
|
||||
|
||||
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await team_members.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await team_members.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.user !== undefined) {
|
||||
await team_members.setUser(
|
||||
|
||||
data.user,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await team_members.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return team_members;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_members = await db.team_members.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of team_members) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of team_members) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return team_members;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_members = await db.team_members.findByPk(id, options);
|
||||
|
||||
await team_members.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await team_members.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return team_members;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const team_members = await db.team_members.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!team_members) {
|
||||
return team_members;
|
||||
}
|
||||
|
||||
const output = team_members.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.team_member_skills_team_member = await team_members.getTeam_member_skills_team_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.resource_allocations_team_member = await team_members.getResource_allocations_team_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.leave_requests_team_member = await team_members.getLeave_requests_team_member({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await team_members.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.user = await team_members.getUser({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await team_members.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'user',
|
||||
|
||||
where: filter.user ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'team_members',
|
||||
'notes',
|
||||
filter.notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.hourly_rateRange) {
|
||||
const [start, end] = filter.hourly_rateRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
hourly_rate: {
|
||||
...where.hourly_rate,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
hourly_rate: {
|
||||
...where.hourly_rate,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.weekly_capacity_hoursRange) {
|
||||
const [start, end] = filter.weekly_capacity_hoursRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
weekly_capacity_hours: {
|
||||
...where.weekly_capacity_hours,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
weekly_capacity_hours: {
|
||||
...where.weekly_capacity_hours,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.employment_type) {
|
||||
where = {
|
||||
...where,
|
||||
employment_type: filter.employment_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.seniority) {
|
||||
where = {
|
||||
...where,
|
||||
seniority: filter.seniority,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.rate_currency) {
|
||||
where = {
|
||||
...where,
|
||||
rate_currency: filter.rate_currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.active) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.team_members.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'team_members',
|
||||
'notes',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.team_members.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
965
backend/src/db/api/tenants.js
Normal file
965
backend/src/db/api/tenants.js
Normal file
@ -0,0 +1,965 @@
|
||||
|
||||
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 TenantsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tenants = await db.tenants.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
name: data.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subdomain: data.subdomain
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tagline: data.tagline
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
primary_color: data.primary_color
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_language: data.default_language
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
timezone: data.timezone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
primary_currency: data.primary_currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
secondary_currencies: data.secondary_currencies
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: data.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: data.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_email: data.billing_email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
support_email: data.support_email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
two_factor_required_for_admins: data.two_factor_required_for_admins
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
ip_whitelist_enabled: data.ip_whitelist_enabled
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
ip_whitelist: data.ip_whitelist
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
business_hours: data.business_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await tenants.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.tenants.getTableName(),
|
||||
belongsToColumn: 'logo',
|
||||
belongsToId: tenants.id,
|
||||
},
|
||||
data.logo,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return tenants;
|
||||
}
|
||||
|
||||
|
||||
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 tenantsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
name: item.name
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
subdomain: item.subdomain
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
tagline: item.tagline
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
primary_color: item.primary_color
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
default_language: item.default_language
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
timezone: item.timezone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
primary_currency: item.primary_currency
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
secondary_currencies: item.secondary_currencies
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
address: item.address
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
phone: item.phone
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billing_email: item.billing_email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
support_email: item.support_email
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
two_factor_required_for_admins: item.two_factor_required_for_admins
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
ip_whitelist_enabled: item.ip_whitelist_enabled
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
ip_whitelist: item.ip_whitelist
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
business_hours: item.business_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const tenants = await db.tenants.bulkCreate(tenantsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < tenants.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.tenants.getTableName(),
|
||||
belongsToColumn: 'logo',
|
||||
belongsToId: tenants[i].id,
|
||||
},
|
||||
data[i].logo,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return tenants;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const tenants = await db.tenants.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.name !== undefined) updatePayload.name = data.name;
|
||||
|
||||
|
||||
if (data.subdomain !== undefined) updatePayload.subdomain = data.subdomain;
|
||||
|
||||
|
||||
if (data.tagline !== undefined) updatePayload.tagline = data.tagline;
|
||||
|
||||
|
||||
if (data.primary_color !== undefined) updatePayload.primary_color = data.primary_color;
|
||||
|
||||
|
||||
if (data.default_language !== undefined) updatePayload.default_language = data.default_language;
|
||||
|
||||
|
||||
if (data.timezone !== undefined) updatePayload.timezone = data.timezone;
|
||||
|
||||
|
||||
if (data.primary_currency !== undefined) updatePayload.primary_currency = data.primary_currency;
|
||||
|
||||
|
||||
if (data.secondary_currencies !== undefined) updatePayload.secondary_currencies = data.secondary_currencies;
|
||||
|
||||
|
||||
if (data.address !== undefined) updatePayload.address = data.address;
|
||||
|
||||
|
||||
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
||||
|
||||
|
||||
if (data.billing_email !== undefined) updatePayload.billing_email = data.billing_email;
|
||||
|
||||
|
||||
if (data.support_email !== undefined) updatePayload.support_email = data.support_email;
|
||||
|
||||
|
||||
if (data.two_factor_required_for_admins !== undefined) updatePayload.two_factor_required_for_admins = data.two_factor_required_for_admins;
|
||||
|
||||
|
||||
if (data.ip_whitelist_enabled !== undefined) updatePayload.ip_whitelist_enabled = data.ip_whitelist_enabled;
|
||||
|
||||
|
||||
if (data.ip_whitelist !== undefined) updatePayload.ip_whitelist = data.ip_whitelist;
|
||||
|
||||
|
||||
if (data.business_hours !== undefined) updatePayload.business_hours = data.business_hours;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await tenants.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await tenants.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.tenants.getTableName(),
|
||||
belongsToColumn: 'logo',
|
||||
belongsToId: tenants.id,
|
||||
},
|
||||
data.logo,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return tenants;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tenants = await db.tenants.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of tenants) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of tenants) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return tenants;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tenants = await db.tenants.findByPk(id, options);
|
||||
|
||||
await tenants.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await tenants.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return tenants;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const tenants = await db.tenants.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!tenants) {
|
||||
return tenants;
|
||||
}
|
||||
|
||||
const output = tenants.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.service_lines_tenant = await tenants.getService_lines_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.services_tenant = await tenants.getServices_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.companies_tenant = await tenants.getCompanies_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contacts_tenant = await tenants.getContacts_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.tags_tenant = await tenants.getTags_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.pipelines_tenant = await tenants.getPipelines_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deal_stages_tenant = await tenants.getDeal_stages_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.leads_tenant = await tenants.getLeads_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deals_tenant = await tenants.getDeals_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_templates_tenant = await tenants.getProject_templates_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.projects_tenant = await tenants.getProjects_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.milestones_tenant = await tenants.getMilestones_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.tasks_tenant = await tenants.getTasks_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.task_dependencies_tenant = await tenants.getTask_dependencies_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.deliverables_tenant = await tenants.getDeliverables_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_risks_tenant = await tenants.getProject_risks_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project_status_reports_tenant = await tenants.getProject_status_reports_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.skills_tenant = await tenants.getSkills_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_members_tenant = await tenants.getTeam_members_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.team_member_skills_tenant = await tenants.getTeam_member_skills_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.resource_allocations_tenant = await tenants.getResource_allocations_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.leave_requests_tenant = await tenants.getLeave_requests_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.public_holidays_tenant = await tenants.getPublic_holidays_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.timesheets_tenant = await tenants.getTimesheets_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.time_entries_tenant = await tenants.getTime_entries_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.products_tenant = await tenants.getProducts_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.contracts_tenant = await tenants.getContracts_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoices_tenant = await tenants.getInvoices_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.invoice_line_items_tenant = await tenants.getInvoice_line_items_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.payments_tenant = await tenants.getPayments_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.support_slas_tenant = await tenants.getSupport_slas_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.support_tickets_tenant = await tenants.getSupport_tickets_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.ticket_comments_tenant = await tenants.getTicket_comments_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.csat_surveys_tenant = await tenants.getCsat_surveys_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.email_templates_tenant = await tenants.getEmail_templates_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.notifications_tenant = await tenants.getNotifications_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.audit_logs_tenant = await tenants.getAudit_logs_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.imports_tenant = await tenants.getImports_tenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
output.logo = await tenants.getLogo({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await tenants.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'logo',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.name) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'name',
|
||||
filter.name,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.subdomain) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'subdomain',
|
||||
filter.subdomain,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.tagline) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'tagline',
|
||||
filter.tagline,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.primary_color) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'primary_color',
|
||||
filter.primary_color,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.timezone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'timezone',
|
||||
filter.timezone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.secondary_currencies) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'secondary_currencies',
|
||||
filter.secondary_currencies,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.address) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'address',
|
||||
filter.address,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.phone) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'phone',
|
||||
filter.phone,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.billing_email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'billing_email',
|
||||
filter.billing_email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.support_email) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'support_email',
|
||||
filter.support_email,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.ip_whitelist) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'ip_whitelist',
|
||||
filter.ip_whitelist,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.business_hours) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'tenants',
|
||||
'business_hours',
|
||||
filter.business_hours,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.default_language) {
|
||||
where = {
|
||||
...where,
|
||||
default_language: filter.default_language,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.primary_currency) {
|
||||
where = {
|
||||
...where,
|
||||
primary_currency: filter.primary_currency,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.two_factor_required_for_admins) {
|
||||
where = {
|
||||
...where,
|
||||
two_factor_required_for_admins: filter.two_factor_required_for_admins,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.ip_whitelist_enabled) {
|
||||
where = {
|
||||
...where,
|
||||
ip_whitelist_enabled: filter.ip_whitelist_enabled,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.tenants.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'tenants',
|
||||
'name',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.tenants.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
633
backend/src/db/api/ticket_comments.js
Normal file
633
backend/src/db/api/ticket_comments.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 Ticket_commentsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const ticket_comments = await db.ticket_comments.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
visibility: data.visibility
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body: data.body
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
posted_at: data.posted_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await ticket_comments.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await ticket_comments.setTicket( data.ticket || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await ticket_comments.setAuthor( data.author || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await ticket_comments.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.ticket_comments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: ticket_comments.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
|
||||
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 ticket_commentsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
visibility: item.visibility
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
body: item.body
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
posted_at: item.posted_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const ticket_comments = await db.ticket_comments.bulkCreate(ticket_commentsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
for (let i = 0; i < ticket_comments.length; i++) {
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.ticket_comments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: ticket_comments[i].id,
|
||||
},
|
||||
data[i].attachments,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const ticket_comments = await db.ticket_comments.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.visibility !== undefined) updatePayload.visibility = data.visibility;
|
||||
|
||||
|
||||
if (data.body !== undefined) updatePayload.body = data.body;
|
||||
|
||||
|
||||
if (data.posted_at !== undefined) updatePayload.posted_at = data.posted_at;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await ticket_comments.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await ticket_comments.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.ticket !== undefined) {
|
||||
await ticket_comments.setTicket(
|
||||
|
||||
data.ticket,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.author !== undefined) {
|
||||
await ticket_comments.setAuthor(
|
||||
|
||||
data.author,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await ticket_comments.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await FileDBApi.replaceRelationFiles(
|
||||
{
|
||||
belongsTo: db.ticket_comments.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
belongsToId: ticket_comments.id,
|
||||
},
|
||||
data.attachments,
|
||||
options,
|
||||
);
|
||||
|
||||
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const ticket_comments = await db.ticket_comments.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of ticket_comments) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of ticket_comments) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const ticket_comments = await db.ticket_comments.findByPk(id, options);
|
||||
|
||||
await ticket_comments.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await ticket_comments.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const ticket_comments = await db.ticket_comments.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!ticket_comments) {
|
||||
return ticket_comments;
|
||||
}
|
||||
|
||||
const output = ticket_comments.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await ticket_comments.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.ticket = await ticket_comments.getTicket({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.author = await ticket_comments.getAuthor({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.attachments = await ticket_comments.getAttachments({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await ticket_comments.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.support_tickets,
|
||||
as: 'ticket',
|
||||
|
||||
where: filter.ticket ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.ticket.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
ticket_number: {
|
||||
[Op.or]: filter.ticket.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'author',
|
||||
|
||||
where: filter.author ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.author.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.author.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
model: db.file,
|
||||
as: 'attachments',
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.body) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'ticket_comments',
|
||||
'body',
|
||||
filter.body,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.posted_atRange) {
|
||||
const [start, end] = filter.posted_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
posted_at: {
|
||||
...where.posted_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
posted_at: {
|
||||
...where.posted_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.visibility) {
|
||||
where = {
|
||||
...where,
|
||||
visibility: filter.visibility,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.ticket_comments.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'ticket_comments',
|
||||
'body',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.ticket_comments.findAll({
|
||||
attributes: [ 'id', 'body' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['body', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.body,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
875
backend/src/db/api/time_entries.js
Normal file
875
backend/src/db/api/time_entries.js
Normal file
@ -0,0 +1,875 @@
|
||||
|
||||
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 Time_entriesDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const time_entries = await db.time_entries.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
started_at: data.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: data.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
duration_hours: data.duration_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billable: data.billable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
work_type: data.work_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: data.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
offline_recorded: data.offline_recorded
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await time_entries.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setUser( data.user || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setTimesheet( data.timesheet || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setCompany( data.company || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setProject( data.project || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setTask( data.task || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setService_line( data.service_line || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
|
||||
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 time_entriesData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
started_at: item.started_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
ended_at: item.ended_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
duration_hours: item.duration_hours
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
billable: item.billable
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
work_type: item.work_type
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
description: item.description
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
offline_recorded: item.offline_recorded
|
||||
||
|
||||
false
|
||||
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const time_entries = await db.time_entries.bulkCreate(time_entriesData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const time_entries = await db.time_entries.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.duration_hours !== undefined) updatePayload.duration_hours = data.duration_hours;
|
||||
|
||||
|
||||
if (data.billable !== undefined) updatePayload.billable = data.billable;
|
||||
|
||||
|
||||
if (data.work_type !== undefined) updatePayload.work_type = data.work_type;
|
||||
|
||||
|
||||
if (data.description !== undefined) updatePayload.description = data.description;
|
||||
|
||||
|
||||
if (data.offline_recorded !== undefined) updatePayload.offline_recorded = data.offline_recorded;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await time_entries.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await time_entries.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.user !== undefined) {
|
||||
await time_entries.setUser(
|
||||
|
||||
data.user,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.timesheet !== undefined) {
|
||||
await time_entries.setTimesheet(
|
||||
|
||||
data.timesheet,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.company !== undefined) {
|
||||
await time_entries.setCompany(
|
||||
|
||||
data.company,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.project !== undefined) {
|
||||
await time_entries.setProject(
|
||||
|
||||
data.project,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.task !== undefined) {
|
||||
await time_entries.setTask(
|
||||
|
||||
data.task,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.service_line !== undefined) {
|
||||
await time_entries.setService_line(
|
||||
|
||||
data.service_line,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await time_entries.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const time_entries = await db.time_entries.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of time_entries) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of time_entries) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const time_entries = await db.time_entries.findByPk(id, options);
|
||||
|
||||
await time_entries.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await time_entries.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const time_entries = await db.time_entries.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!time_entries) {
|
||||
return time_entries;
|
||||
}
|
||||
|
||||
const output = time_entries.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await time_entries.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.user = await time_entries.getUser({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.timesheet = await time_entries.getTimesheet({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.company = await time_entries.getCompany({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.project = await time_entries.getProject({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.task = await time_entries.getTask({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.service_line = await time_entries.getService_line({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await time_entries.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'user',
|
||||
|
||||
where: filter.user ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.timesheets,
|
||||
as: 'timesheet',
|
||||
|
||||
where: filter.timesheet ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.timesheet.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
status: {
|
||||
[Op.or]: filter.timesheet.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.companies,
|
||||
as: 'company',
|
||||
|
||||
where: filter.company ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
legal_name: {
|
||||
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.projects,
|
||||
as: 'project',
|
||||
|
||||
where: filter.project ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.tasks,
|
||||
as: 'task',
|
||||
|
||||
where: filter.task ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.task.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
title: {
|
||||
[Op.or]: filter.task.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.service_lines,
|
||||
as: 'service_line',
|
||||
|
||||
where: filter.service_line ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.service_line.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name_fr: {
|
||||
[Op.or]: filter.service_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.description) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'time_entries',
|
||||
'description',
|
||||
filter.description,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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.duration_hoursRange) {
|
||||
const [start, end] = filter.duration_hoursRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
duration_hours: {
|
||||
...where.duration_hours,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
duration_hours: {
|
||||
...where.duration_hours,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (filter.active !== undefined) {
|
||||
where = {
|
||||
...where,
|
||||
active: filter.active === true || filter.active === 'true'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.billable) {
|
||||
where = {
|
||||
...where,
|
||||
billable: filter.billable,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.work_type) {
|
||||
where = {
|
||||
...where,
|
||||
work_type: filter.work_type,
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.offline_recorded) {
|
||||
where = {
|
||||
...where,
|
||||
offline_recorded: filter.offline_recorded,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.time_entries.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'time_entries',
|
||||
'description',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.time_entries.findAll({
|
||||
attributes: [ 'id', 'description' ],
|
||||
where,
|
||||
limit: limit ? Number(limit) : undefined,
|
||||
offset: offset ? Number(offset) : undefined,
|
||||
orderBy: [['description', 'ASC']],
|
||||
});
|
||||
|
||||
return records.map((record) => ({
|
||||
id: record.id,
|
||||
label: record.description,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
706
backend/src/db/api/timesheets.js
Normal file
706
backend/src/db/api/timesheets.js
Normal file
@ -0,0 +1,706 @@
|
||||
|
||||
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 TimesheetsDBApi {
|
||||
|
||||
|
||||
|
||||
static async create(data, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const timesheets = await db.timesheets.create(
|
||||
{
|
||||
id: data.id || undefined,
|
||||
|
||||
week_start_at: data.week_start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
week_end_at: data.week_end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: data.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
submitted_at: data.submitted_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approved_at: data.approved_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approval_notes: data.approval_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: data.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
},
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
|
||||
await timesheets.setTenant( data.tenant || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await timesheets.setUser( data.user || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await timesheets.setApprover( data.approver || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await timesheets.setOrganizations( data.organizations || null, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
|
||||
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 timesheetsData = data.map((item, index) => ({
|
||||
id: item.id || undefined,
|
||||
|
||||
week_start_at: item.week_start_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
week_end_at: item.week_end_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
status: item.status
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
submitted_at: item.submitted_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approved_at: item.approved_at
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
approval_notes: item.approval_notes
|
||||
||
|
||||
null
|
||||
,
|
||||
|
||||
importHash: item.importHash || null,
|
||||
createdById: currentUser.id,
|
||||
updatedById: currentUser.id,
|
||||
createdAt: new Date(Date.now() + index * 1000),
|
||||
}));
|
||||
|
||||
// Bulk create items
|
||||
const timesheets = await db.timesheets.bulkCreate(timesheetsData, { transaction });
|
||||
|
||||
// For each item created, replace relation files
|
||||
|
||||
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
static async update(id, data, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
const globalAccess = currentUser.app_role?.globalAccess;
|
||||
|
||||
const timesheets = await db.timesheets.findByPk(id, {}, {transaction});
|
||||
|
||||
|
||||
|
||||
|
||||
const updatePayload = {};
|
||||
|
||||
if (data.week_start_at !== undefined) updatePayload.week_start_at = data.week_start_at;
|
||||
|
||||
|
||||
if (data.week_end_at !== undefined) updatePayload.week_end_at = data.week_end_at;
|
||||
|
||||
|
||||
if (data.status !== undefined) updatePayload.status = data.status;
|
||||
|
||||
|
||||
if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at;
|
||||
|
||||
|
||||
if (data.approved_at !== undefined) updatePayload.approved_at = data.approved_at;
|
||||
|
||||
|
||||
if (data.approval_notes !== undefined) updatePayload.approval_notes = data.approval_notes;
|
||||
|
||||
|
||||
updatePayload.updatedById = currentUser.id;
|
||||
|
||||
await timesheets.update(updatePayload, {transaction});
|
||||
|
||||
|
||||
|
||||
if (data.tenant !== undefined) {
|
||||
await timesheets.setTenant(
|
||||
|
||||
data.tenant,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.user !== undefined) {
|
||||
await timesheets.setUser(
|
||||
|
||||
data.user,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.approver !== undefined) {
|
||||
await timesheets.setApprover(
|
||||
|
||||
data.approver,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (data.organizations !== undefined) {
|
||||
await timesheets.setOrganizations(
|
||||
|
||||
data.organizations,
|
||||
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
static async deleteByIds(ids, options) {
|
||||
const currentUser = (options && options.currentUser) || { id: null };
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const timesheets = await db.timesheets.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids,
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
await db.sequelize.transaction(async (transaction) => {
|
||||
for (const record of timesheets) {
|
||||
await record.update(
|
||||
{deletedBy: currentUser.id},
|
||||
{transaction}
|
||||
);
|
||||
}
|
||||
for (const record of timesheets) {
|
||||
await record.destroy({transaction});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
static async remove(id, options) {
|
||||
const currentUser = (options && options.currentUser) || {id: null};
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const timesheets = await db.timesheets.findByPk(id, options);
|
||||
|
||||
await timesheets.update({
|
||||
deletedBy: currentUser.id
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await timesheets.destroy({
|
||||
transaction
|
||||
});
|
||||
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
static async findBy(where, options) {
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
const timesheets = await db.timesheets.findOne(
|
||||
{ where },
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
if (!timesheets) {
|
||||
return timesheets;
|
||||
}
|
||||
|
||||
const output = timesheets.get({plain: true});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.time_entries_timesheet = await timesheets.getTime_entries_timesheet({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output.tenant = await timesheets.getTenant({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.user = await timesheets.getUser({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.approver = await timesheets.getApprover({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
output.organizations = await timesheets.getOrganizations({
|
||||
transaction
|
||||
});
|
||||
|
||||
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
static async findAll(
|
||||
filter,
|
||||
globalAccess, options
|
||||
) {
|
||||
const limit = filter.limit || 0;
|
||||
let offset = 0;
|
||||
let where = {};
|
||||
const currentPage = +filter.page;
|
||||
|
||||
|
||||
const user = (options && options.currentUser) || null;
|
||||
const userOrganizations = (user && user.organizations?.id) || null;
|
||||
|
||||
|
||||
|
||||
if (userOrganizations) {
|
||||
if (options?.currentUser?.organizationsId) {
|
||||
where.organizationsId = options.currentUser.organizationsId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset = currentPage * limit;
|
||||
|
||||
const orderBy = null;
|
||||
|
||||
const transaction = (options && options.transaction) || undefined;
|
||||
|
||||
let include = [
|
||||
|
||||
{
|
||||
model: db.tenants,
|
||||
as: 'tenant',
|
||||
|
||||
where: filter.tenant ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
name: {
|
||||
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'user',
|
||||
|
||||
where: filter.user ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.users,
|
||||
as: 'approver',
|
||||
|
||||
where: filter.approver ? {
|
||||
[Op.or]: [
|
||||
{ id: { [Op.in]: filter.approver.split('|').map(term => Utils.uuid(term)) } },
|
||||
{
|
||||
firstName: {
|
||||
[Op.or]: filter.approver.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
||||
}
|
||||
},
|
||||
]
|
||||
} : {},
|
||||
|
||||
},
|
||||
|
||||
{
|
||||
model: db.organizations,
|
||||
as: 'organizations',
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
||||
if (filter) {
|
||||
if (filter.id) {
|
||||
where = {
|
||||
...where,
|
||||
['id']: Utils.uuid(filter.id),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (filter.approval_notes) {
|
||||
where = {
|
||||
...where,
|
||||
[Op.and]: Utils.ilike(
|
||||
'timesheets',
|
||||
'approval_notes',
|
||||
filter.approval_notes,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (filter.week_start_atRange) {
|
||||
const [start, end] = filter.week_start_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
week_start_at: {
|
||||
...where.week_start_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
week_start_at: {
|
||||
...where.week_start_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.week_end_atRange) {
|
||||
const [start, end] = filter.week_end_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
week_end_at: {
|
||||
...where.week_end_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
week_end_at: {
|
||||
...where.week_end_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.submitted_atRange) {
|
||||
const [start, end] = filter.submitted_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
submitted_at: {
|
||||
...where.submitted_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
submitted_at: {
|
||||
...where.submitted_at,
|
||||
[Op.lte]: end,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.approved_atRange) {
|
||||
const [start, end] = filter.approved_atRange;
|
||||
|
||||
if (start !== undefined && start !== null && start !== '') {
|
||||
where = {
|
||||
...where,
|
||||
approved_at: {
|
||||
...where.approved_at,
|
||||
[Op.gte]: start,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (end !== undefined && end !== null && end !== '') {
|
||||
where = {
|
||||
...where,
|
||||
approved_at: {
|
||||
...where.approved_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.organizations) {
|
||||
const listItems = filter.organizations.split('|').map(item => {
|
||||
return Utils.uuid(item)
|
||||
});
|
||||
|
||||
where = {
|
||||
...where,
|
||||
organizationsId: {[Op.or]: listItems}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (globalAccess) {
|
||||
delete where.organizationsId;
|
||||
}
|
||||
|
||||
|
||||
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.timesheets.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, globalAccess, organizationId,) {
|
||||
let where = {};
|
||||
|
||||
|
||||
if (!globalAccess && organizationId) {
|
||||
where.organizationId = organizationId;
|
||||
}
|
||||
|
||||
|
||||
if (query) {
|
||||
where = {
|
||||
[Op.or]: [
|
||||
{ ['id']: Utils.uuid(query) },
|
||||
Utils.ilike(
|
||||
'timesheets',
|
||||
'status',
|
||||
query,
|
||||
),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const records = await db.timesheets.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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
1090
backend/src/db/api/users.js
Normal file
1090
backend/src/db/api/users.js
Normal file
File diff suppressed because it is too large
Load Diff
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_tsc_fleetos',
|
||||
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',
|
||||
}
|
||||
};
|
||||
12711
backend/src/db/migrations/1778443325194.js
Normal file
12711
backend/src/db/migrations/1778443325194.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;
|
||||
}
|
||||
},
|
||||
};
|
||||
201
backend/src/db/models/audit_logs.js
Normal file
201
backend/src/db/models/audit_logs.js
Normal file
@ -0,0 +1,201 @@
|
||||
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_logs = sequelize.define(
|
||||
'audit_logs',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
action: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"create",
|
||||
|
||||
|
||||
"update",
|
||||
|
||||
|
||||
"delete",
|
||||
|
||||
|
||||
"login",
|
||||
|
||||
|
||||
"logout",
|
||||
|
||||
|
||||
"export",
|
||||
|
||||
|
||||
"impersonate",
|
||||
|
||||
|
||||
"access_denied"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
entity: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
entity_key: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
ip_address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
user_agent: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
occurred_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
details: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
audit_logs.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_logs.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.audit_logs.belongsTo(db.users, {
|
||||
as: 'actor',
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.audit_logs.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.audit_logs.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.audit_logs.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return audit_logs;
|
||||
};
|
||||
|
||||
|
||||
314
backend/src/db/models/companies.js
Normal file
314
backend/src/db/models/companies.js
Normal file
@ -0,0 +1,314 @@
|
||||
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 companies = sequelize.define(
|
||||
'companies',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
legal_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
trade_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
rccm: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
tax_id: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
industry: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
size_band: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"1_10",
|
||||
|
||||
|
||||
"11_50",
|
||||
|
||||
|
||||
"51_200",
|
||||
|
||||
|
||||
"201_500",
|
||||
|
||||
|
||||
"501_1000",
|
||||
|
||||
|
||||
"1000_plus"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
annual_revenue: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
website: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
address: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
city: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
country: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
lifecycle_stage: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"prospect",
|
||||
|
||||
|
||||
"lead",
|
||||
|
||||
|
||||
"customer",
|
||||
|
||||
|
||||
"champion",
|
||||
|
||||
|
||||
"lost",
|
||||
|
||||
|
||||
"inactive"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
companies.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.contacts, {
|
||||
as: 'contacts_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.deals, {
|
||||
as: 'deals_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.projects, {
|
||||
as: 'projects_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.time_entries, {
|
||||
as: 'time_entries_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.contracts, {
|
||||
as: 'contracts_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.companies.hasMany(db.invoices, {
|
||||
as: 'invoices_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.companies.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.companies.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.companies.belongsTo(db.companies, {
|
||||
as: 'parent_company',
|
||||
foreignKey: {
|
||||
name: 'parent_companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.companies.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.companies.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.companies.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return companies;
|
||||
};
|
||||
|
||||
|
||||
270
backend/src/db/models/contacts.js
Normal file
270
backend/src/db/models/contacts.js
Normal file
@ -0,0 +1,270 @@
|
||||
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 contacts = sequelize.define(
|
||||
'contacts',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
first_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
last_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
email: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
phone: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
job_title: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
whatsapp: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
linkedin_url: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
preferred_channel: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
allow_email: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
allow_sms: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
allow_whatsapp: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
lifecycle_stage: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"prospect",
|
||||
|
||||
|
||||
"lead",
|
||||
|
||||
|
||||
"customer",
|
||||
|
||||
|
||||
"champion",
|
||||
|
||||
|
||||
"lost",
|
||||
|
||||
|
||||
"inactive"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
contacts.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.contacts.hasMany(db.deals, {
|
||||
as: 'deals_primary_contact',
|
||||
foreignKey: {
|
||||
name: 'primary_contactId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.contacts.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_contact',
|
||||
foreignKey: {
|
||||
name: 'contactId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.contacts.hasMany(db.csat_surveys, {
|
||||
as: 'csat_surveys_contact',
|
||||
foreignKey: {
|
||||
name: 'contactId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.contacts.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.contacts.belongsTo(db.companies, {
|
||||
as: 'company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.contacts.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.contacts.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.contacts.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return contacts;
|
||||
};
|
||||
|
||||
|
||||
298
backend/src/db/models/contracts.js
Normal file
298
backend/src/db/models/contracts.js
Normal file
@ -0,0 +1,298 @@
|
||||
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 contracts = sequelize.define(
|
||||
'contracts',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
contract_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"maintenance",
|
||||
|
||||
|
||||
"msa",
|
||||
|
||||
|
||||
"sow",
|
||||
|
||||
|
||||
"nda",
|
||||
|
||||
|
||||
"hosting",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
billing_model: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"fixed_price",
|
||||
|
||||
|
||||
"time_material",
|
||||
|
||||
|
||||
"recurring"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
auto_renew: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
renewal_notice_days: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"draft",
|
||||
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"suspended",
|
||||
|
||||
|
||||
"expired",
|
||||
|
||||
|
||||
"terminated"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
contracts.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.contracts.hasMany(db.invoices, {
|
||||
as: 'invoices_contract',
|
||||
foreignKey: {
|
||||
name: 'contractId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.contracts.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_contract',
|
||||
foreignKey: {
|
||||
name: 'contractId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.contracts.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.contracts.belongsTo(db.companies, {
|
||||
as: 'company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.contracts.belongsTo(db.service_lines, {
|
||||
as: 'service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.contracts.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.contracts.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.contracts.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return contracts;
|
||||
};
|
||||
|
||||
|
||||
161
backend/src/db/models/csat_surveys.js
Normal file
161
backend/src/db/models/csat_surveys.js
Normal file
@ -0,0 +1,161 @@
|
||||
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 csat_surveys = sequelize.define(
|
||||
'csat_surveys',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
score: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
feedback: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sent_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
responded_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
csat_surveys.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.csat_surveys.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.csat_surveys.belongsTo(db.support_tickets, {
|
||||
as: 'ticket',
|
||||
foreignKey: {
|
||||
name: 'ticketId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.csat_surveys.belongsTo(db.contacts, {
|
||||
as: 'contact',
|
||||
foreignKey: {
|
||||
name: 'contactId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.csat_surveys.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.csat_surveys.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.csat_surveys.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return csat_surveys;
|
||||
};
|
||||
|
||||
|
||||
174
backend/src/db/models/deal_stages.js
Normal file
174
backend/src/db/models/deal_stages.js
Normal file
@ -0,0 +1,174 @@
|
||||
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 deal_stages = sequelize.define(
|
||||
'deal_stages',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
probability_percent: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sort_order: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
is_closed_won: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
is_closed_lost: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
deal_stages.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.deal_stages.hasMany(db.deals, {
|
||||
as: 'deals_stage',
|
||||
foreignKey: {
|
||||
name: 'stageId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.deal_stages.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deal_stages.belongsTo(db.pipelines, {
|
||||
as: 'pipeline',
|
||||
foreignKey: {
|
||||
name: 'pipelineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deal_stages.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.deal_stages.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.deal_stages.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return deal_stages;
|
||||
};
|
||||
|
||||
|
||||
266
backend/src/db/models/deals.js
Normal file
266
backend/src/db/models/deals.js
Normal file
@ -0,0 +1,266 @@
|
||||
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 deals = sequelize.define(
|
||||
'deals',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
expected_close_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"open",
|
||||
|
||||
|
||||
"won",
|
||||
|
||||
|
||||
"lost"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
loss_reason: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
won_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
lost_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
deals.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.deals.hasMany(db.projects, {
|
||||
as: 'projects_deal',
|
||||
foreignKey: {
|
||||
name: 'dealId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.deals.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.pipelines, {
|
||||
as: 'pipeline',
|
||||
foreignKey: {
|
||||
name: 'pipelineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.deal_stages, {
|
||||
as: 'stage',
|
||||
foreignKey: {
|
||||
name: 'stageId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.companies, {
|
||||
as: 'company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.contacts, {
|
||||
as: 'primary_contact',
|
||||
foreignKey: {
|
||||
name: 'primary_contactId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.service_lines, {
|
||||
as: 'service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.users, {
|
||||
as: 'owner',
|
||||
foreignKey: {
|
||||
name: 'ownerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.deals.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.deals.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return deals;
|
||||
};
|
||||
|
||||
|
||||
188
backend/src/db/models/deliverables.js
Normal file
188
backend/src/db/models/deliverables.js
Normal file
@ -0,0 +1,188 @@
|
||||
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 deliverables = sequelize.define(
|
||||
'deliverables',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pending",
|
||||
|
||||
|
||||
"in_progress",
|
||||
|
||||
|
||||
"submitted",
|
||||
|
||||
|
||||
"accepted",
|
||||
|
||||
|
||||
"rejected"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
due_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
client_feedback: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
deliverables.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.deliverables.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deliverables.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.deliverables.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.deliverables.hasMany(db.file, {
|
||||
as: 'attachments',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.deliverables.getTableName(),
|
||||
belongsToColumn: 'attachments',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.deliverables.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.deliverables.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return deliverables;
|
||||
};
|
||||
|
||||
|
||||
193
backend/src/db/models/email_templates.js
Normal file
193
backend/src/db/models/email_templates.js
Normal file
@ -0,0 +1,193 @@
|
||||
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 email_templates = sequelize.define(
|
||||
'email_templates',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
template_key: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"welcome",
|
||||
|
||||
|
||||
"password_reset",
|
||||
|
||||
|
||||
"invoice_sent",
|
||||
|
||||
|
||||
"ticket_assigned",
|
||||
|
||||
|
||||
"deal_stage_changed",
|
||||
|
||||
|
||||
"timesheet_reminder",
|
||||
|
||||
|
||||
"ticket_resolution_survey"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
subject_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
subject_en: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
body_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
body_en: {
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
email_templates.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.email_templates.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.email_templates.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.email_templates.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.email_templates.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return email_templates;
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
};
|
||||
236
backend/src/db/models/imports.js
Normal file
236
backend/src/db/models/imports.js
Normal file
@ -0,0 +1,236 @@
|
||||
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 imports = sequelize.define(
|
||||
'imports',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
entity_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"companies",
|
||||
|
||||
|
||||
"contacts",
|
||||
|
||||
|
||||
"leads",
|
||||
|
||||
|
||||
"deals",
|
||||
|
||||
|
||||
"projects",
|
||||
|
||||
|
||||
"tasks",
|
||||
|
||||
|
||||
"time_entries",
|
||||
|
||||
|
||||
"invoices",
|
||||
|
||||
|
||||
"support_tickets"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pending",
|
||||
|
||||
|
||||
"processing",
|
||||
|
||||
|
||||
"completed",
|
||||
|
||||
|
||||
"failed"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
total_rows: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
success_rows: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
failed_rows: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
error_report: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
started_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
finished_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
imports.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.imports.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.imports.belongsTo(db.users, {
|
||||
as: 'requested_by',
|
||||
foreignKey: {
|
||||
name: 'requested_byId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.imports.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.imports.hasMany(db.file, {
|
||||
as: 'source_file',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.imports.getTableName(),
|
||||
belongsToColumn: 'source_file',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.imports.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.imports.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return imports;
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
171
backend/src/db/models/invoice_line_items.js
Normal file
171
backend/src/db/models/invoice_line_items.js
Normal file
@ -0,0 +1,171 @@
|
||||
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 invoice_line_items = sequelize.define(
|
||||
'invoice_line_items',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
quantity: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
unit_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
line_total: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
taxable: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
invoice_line_items.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.invoice_line_items.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoice_line_items.belongsTo(db.invoices, {
|
||||
as: 'invoice',
|
||||
foreignKey: {
|
||||
name: 'invoiceId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoice_line_items.belongsTo(db.products, {
|
||||
as: 'product',
|
||||
foreignKey: {
|
||||
name: 'productId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoice_line_items.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.invoice_line_items.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.invoice_line_items.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return invoice_line_items;
|
||||
};
|
||||
|
||||
|
||||
312
backend/src/db/models/invoices.js
Normal file
312
backend/src/db/models/invoices.js
Normal file
@ -0,0 +1,312 @@
|
||||
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 invoices = sequelize.define(
|
||||
'invoices',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
invoice_number: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"time_based",
|
||||
|
||||
|
||||
"fixed_price",
|
||||
|
||||
|
||||
"recurring",
|
||||
|
||||
|
||||
"credit_note"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"draft",
|
||||
|
||||
|
||||
"sent",
|
||||
|
||||
|
||||
"partial",
|
||||
|
||||
|
||||
"paid",
|
||||
|
||||
|
||||
"overdue",
|
||||
|
||||
|
||||
"void"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
issue_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
due_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
subtotal: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
tax_rate_percent: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
tax_amount: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
total: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
amount_paid: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
public_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
internal_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
invoices.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.invoices.hasMany(db.invoice_line_items, {
|
||||
as: 'invoice_line_items_invoice',
|
||||
foreignKey: {
|
||||
name: 'invoiceId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.invoices.hasMany(db.payments, {
|
||||
as: 'payments_invoice',
|
||||
foreignKey: {
|
||||
name: 'invoiceId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.invoices.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoices.belongsTo(db.companies, {
|
||||
as: 'company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoices.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoices.belongsTo(db.contracts, {
|
||||
as: 'contract',
|
||||
foreignKey: {
|
||||
name: 'contractId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.invoices.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.invoices.hasMany(db.file, {
|
||||
as: 'pdf_file',
|
||||
foreignKey: 'belongsToId',
|
||||
constraints: false,
|
||||
scope: {
|
||||
belongsTo: db.invoices.getTableName(),
|
||||
belongsToColumn: 'pdf_file',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
db.invoices.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.invoices.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return invoices;
|
||||
};
|
||||
|
||||
|
||||
232
backend/src/db/models/leads.js
Normal file
232
backend/src/db/models/leads.js
Normal file
@ -0,0 +1,232 @@
|
||||
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 leads = sequelize.define(
|
||||
'leads',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
full_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
email: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
phone: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
company_name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
message: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
source: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"website_form",
|
||||
|
||||
|
||||
"referral",
|
||||
|
||||
|
||||
"outbound",
|
||||
|
||||
|
||||
"event",
|
||||
|
||||
|
||||
"partner",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
score: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"new",
|
||||
|
||||
|
||||
"qualified",
|
||||
|
||||
|
||||
"disqualified",
|
||||
|
||||
|
||||
"converted"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
captured_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
leads.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.leads.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leads.belongsTo(db.services, {
|
||||
as: 'service_interest',
|
||||
foreignKey: {
|
||||
name: 'service_interestId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leads.belongsTo(db.users, {
|
||||
as: 'owner',
|
||||
foreignKey: {
|
||||
name: 'ownerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leads.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.leads.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.leads.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return leads;
|
||||
};
|
||||
|
||||
|
||||
201
backend/src/db/models/leave_requests.js
Normal file
201
backend/src/db/models/leave_requests.js
Normal file
@ -0,0 +1,201 @@
|
||||
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 leave_requests = sequelize.define(
|
||||
'leave_requests',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
leave_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"vacation",
|
||||
|
||||
|
||||
"sick",
|
||||
|
||||
|
||||
"public_holiday",
|
||||
|
||||
|
||||
"training",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"requested",
|
||||
|
||||
|
||||
"approved",
|
||||
|
||||
|
||||
"rejected",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
reason: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
leave_requests.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.leave_requests.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leave_requests.belongsTo(db.team_members, {
|
||||
as: 'team_member',
|
||||
foreignKey: {
|
||||
name: 'team_memberId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leave_requests.belongsTo(db.users, {
|
||||
as: 'approver',
|
||||
foreignKey: {
|
||||
name: 'approverId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.leave_requests.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.leave_requests.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.leave_requests.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return leave_requests;
|
||||
};
|
||||
|
||||
|
||||
190
backend/src/db/models/milestones.js
Normal file
190
backend/src/db/models/milestones.js
Normal file
@ -0,0 +1,190 @@
|
||||
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 milestones = sequelize.define(
|
||||
'milestones',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
due_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"not_started",
|
||||
|
||||
|
||||
"in_progress",
|
||||
|
||||
|
||||
"done",
|
||||
|
||||
|
||||
"blocked"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
sort_order: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
milestones.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.milestones.hasMany(db.tasks, {
|
||||
as: 'tasks_milestone',
|
||||
foreignKey: {
|
||||
name: 'milestoneId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.milestones.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.milestones.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.milestones.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.milestones.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.milestones.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return milestones;
|
||||
};
|
||||
|
||||
|
||||
217
backend/src/db/models/notifications.js
Normal file
217
backend/src/db/models/notifications.js
Normal file
@ -0,0 +1,217 @@
|
||||
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 notifications = sequelize.define(
|
||||
'notifications',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
channel: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"in_app",
|
||||
|
||||
|
||||
"email"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
event_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"invoice_sent",
|
||||
|
||||
|
||||
"invoice_overdue",
|
||||
|
||||
|
||||
"ticket_assigned",
|
||||
|
||||
|
||||
"ticket_sla_breached",
|
||||
|
||||
|
||||
"deal_stage_changed",
|
||||
|
||||
|
||||
"timesheet_missing",
|
||||
|
||||
|
||||
"project_status_changed"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
title: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
body: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
link_url: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
read: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sent_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
read_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
notifications.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.notifications.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.notifications.belongsTo(db.users, {
|
||||
as: 'user',
|
||||
foreignKey: {
|
||||
name: 'userId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.notifications.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.notifications.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.notifications.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return notifications;
|
||||
};
|
||||
|
||||
|
||||
428
backend/src/db/models/organizations.js
Normal file
428
backend/src/db/models/organizations.js
Normal file
@ -0,0 +1,428 @@
|
||||
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 organizations = sequelize.define(
|
||||
'organizations',
|
||||
{
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
organizations.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
db.organizations.hasMany(db.users, {
|
||||
as: 'users_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.organizations.hasMany(db.tenants, {
|
||||
as: 'tenants_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.service_lines, {
|
||||
as: 'service_lines_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.services, {
|
||||
as: 'services_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.companies, {
|
||||
as: 'companies_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.contacts, {
|
||||
as: 'contacts_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.tags, {
|
||||
as: 'tags_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.pipelines, {
|
||||
as: 'pipelines_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.deal_stages, {
|
||||
as: 'deal_stages_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.leads, {
|
||||
as: 'leads_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.deals, {
|
||||
as: 'deals_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.project_templates, {
|
||||
as: 'project_templates_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.projects, {
|
||||
as: 'projects_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.milestones, {
|
||||
as: 'milestones_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.tasks, {
|
||||
as: 'tasks_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.task_dependencies, {
|
||||
as: 'task_dependencies_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.deliverables, {
|
||||
as: 'deliverables_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.project_risks, {
|
||||
as: 'project_risks_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.project_status_reports, {
|
||||
as: 'project_status_reports_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.skills, {
|
||||
as: 'skills_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.team_members, {
|
||||
as: 'team_members_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.team_member_skills, {
|
||||
as: 'team_member_skills_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.resource_allocations, {
|
||||
as: 'resource_allocations_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.leave_requests, {
|
||||
as: 'leave_requests_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.public_holidays, {
|
||||
as: 'public_holidays_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.timesheets, {
|
||||
as: 'timesheets_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.time_entries, {
|
||||
as: 'time_entries_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.products, {
|
||||
as: 'products_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.contracts, {
|
||||
as: 'contracts_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.invoices, {
|
||||
as: 'invoices_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.invoice_line_items, {
|
||||
as: 'invoice_line_items_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.payments, {
|
||||
as: 'payments_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.support_slas, {
|
||||
as: 'support_slas_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.ticket_comments, {
|
||||
as: 'ticket_comments_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.csat_surveys, {
|
||||
as: 'csat_surveys_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.email_templates, {
|
||||
as: 'email_templates_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.notifications, {
|
||||
as: 'notifications_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.audit_logs, {
|
||||
as: 'audit_logs_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.organizations.hasMany(db.imports, {
|
||||
as: 'imports_organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.organizations.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.organizations.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return organizations;
|
||||
};
|
||||
|
||||
|
||||
225
backend/src/db/models/payments.js
Normal file
225
backend/src/db/models/payments.js
Normal file
@ -0,0 +1,225 @@
|
||||
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 payments = sequelize.define(
|
||||
'payments',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
paid_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
provider: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"stripe",
|
||||
|
||||
|
||||
"bank_transfer",
|
||||
|
||||
|
||||
"cash",
|
||||
|
||||
|
||||
"mobile_money",
|
||||
|
||||
|
||||
"other"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
provider_reference: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"pending",
|
||||
|
||||
|
||||
"succeeded",
|
||||
|
||||
|
||||
"failed",
|
||||
|
||||
|
||||
"refunded"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
payments.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.payments.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.payments.belongsTo(db.invoices, {
|
||||
as: 'invoice',
|
||||
foreignKey: {
|
||||
name: 'invoiceId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.payments.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.payments.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.payments.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return payments;
|
||||
};
|
||||
|
||||
|
||||
108
backend/src/db/models/permissions.js
Normal file
108
backend/src/db/models/permissions.js
Normal file
@ -0,0 +1,108 @@
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
179
backend/src/db/models/pipelines.js
Normal file
179
backend/src/db/models/pipelines.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 pipelines = sequelize.define(
|
||||
'pipelines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
pipeline_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"sales",
|
||||
|
||||
|
||||
"renewals",
|
||||
|
||||
|
||||
"upsell",
|
||||
|
||||
|
||||
"partnership"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sort_order: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
pipelines.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.pipelines.hasMany(db.deal_stages, {
|
||||
as: 'deal_stages_pipeline',
|
||||
foreignKey: {
|
||||
name: 'pipelineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.pipelines.hasMany(db.deals, {
|
||||
as: 'deals_pipeline',
|
||||
foreignKey: {
|
||||
name: 'pipelineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.pipelines.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.pipelines.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.pipelines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.pipelines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return pipelines;
|
||||
};
|
||||
|
||||
|
||||
223
backend/src/db/models/products.js
Normal file
223
backend/src/db/models/products.js
Normal file
@ -0,0 +1,223 @@
|
||||
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 products = sequelize.define(
|
||||
'products',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
sku: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
unit_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
unit: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"hour",
|
||||
|
||||
|
||||
"day",
|
||||
|
||||
|
||||
"week",
|
||||
|
||||
|
||||
"month",
|
||||
|
||||
|
||||
"fixed"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
taxable: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
products.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.products.hasMany(db.invoice_line_items, {
|
||||
as: 'invoice_line_items_product',
|
||||
foreignKey: {
|
||||
name: 'productId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.products.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.products.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.products.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.products.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return products;
|
||||
};
|
||||
|
||||
|
||||
214
backend/src/db/models/project_risks.js
Normal file
214
backend/src/db/models/project_risks.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 project_risks = sequelize.define(
|
||||
'project_risks',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
title: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
impact: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"low",
|
||||
|
||||
|
||||
"medium",
|
||||
|
||||
|
||||
"high",
|
||||
|
||||
|
||||
"critical"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
likelihood: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"low",
|
||||
|
||||
|
||||
"medium",
|
||||
|
||||
|
||||
"high"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"open",
|
||||
|
||||
|
||||
"mitigating",
|
||||
|
||||
|
||||
"closed"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
mitigation_plan: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
project_risks.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.project_risks.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_risks.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_risks.belongsTo(db.users, {
|
||||
as: 'owner',
|
||||
foreignKey: {
|
||||
name: 'ownerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_risks.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.project_risks.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.project_risks.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return project_risks;
|
||||
};
|
||||
|
||||
|
||||
194
backend/src/db/models/project_status_reports.js
Normal file
194
backend/src/db/models/project_status_reports.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 project_status_reports = sequelize.define(
|
||||
'project_status_reports',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
reporting_week_start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
reporting_week_end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
overall_status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"green",
|
||||
|
||||
|
||||
"amber",
|
||||
|
||||
|
||||
"red"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
accomplishments: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
next_steps: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
risks_issues: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
client_notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
project_status_reports.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.project_status_reports.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_status_reports.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_status_reports.belongsTo(db.users, {
|
||||
as: 'author',
|
||||
foreignKey: {
|
||||
name: 'authorId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_status_reports.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.project_status_reports.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.project_status_reports.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return project_status_reports;
|
||||
};
|
||||
|
||||
|
||||
190
backend/src/db/models/project_templates.js
Normal file
190
backend/src/db/models/project_templates.js
Normal file
@ -0,0 +1,190 @@
|
||||
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 project_templates = sequelize.define(
|
||||
'project_templates',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
default_duration_weeks: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
default_budget: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
billing_model: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"fixed_price",
|
||||
|
||||
|
||||
"time_material",
|
||||
|
||||
|
||||
"recurring"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
project_templates.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.project_templates.hasMany(db.projects, {
|
||||
as: 'projects_template',
|
||||
foreignKey: {
|
||||
name: 'templateId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.project_templates.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_templates.belongsTo(db.service_lines, {
|
||||
as: 'service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.project_templates.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.project_templates.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.project_templates.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return project_templates;
|
||||
};
|
||||
|
||||
|
||||
383
backend/src/db/models/projects.js
Normal file
383
backend/src/db/models/projects.js
Normal file
@ -0,0 +1,383 @@
|
||||
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 projects = sequelize.define(
|
||||
'projects',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
code: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"planned",
|
||||
|
||||
|
||||
"active",
|
||||
|
||||
|
||||
"on_hold",
|
||||
|
||||
|
||||
"completed",
|
||||
|
||||
|
||||
"cancelled"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
delivery_method: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"agile",
|
||||
|
||||
|
||||
"waterfall",
|
||||
|
||||
|
||||
"hybrid"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
budget_amount: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
budget_currency: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"USD",
|
||||
|
||||
|
||||
"CDF",
|
||||
|
||||
|
||||
"EUR",
|
||||
|
||||
|
||||
"XAF",
|
||||
|
||||
|
||||
"XOF"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
external_costs: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
estimated_hours: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
actual_hours: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
risk_level: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"low",
|
||||
|
||||
|
||||
"medium",
|
||||
|
||||
|
||||
"high",
|
||||
|
||||
|
||||
"critical"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
summary: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
projects.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.milestones, {
|
||||
as: 'milestones_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.projects.hasMany(db.tasks, {
|
||||
as: 'tasks_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.deliverables, {
|
||||
as: 'deliverables_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.projects.hasMany(db.project_risks, {
|
||||
as: 'project_risks_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.projects.hasMany(db.project_status_reports, {
|
||||
as: 'project_status_reports_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.resource_allocations, {
|
||||
as: 'resource_allocations_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.time_entries, {
|
||||
as: 'time_entries_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.invoices, {
|
||||
as: 'invoices_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.projects.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.companies, {
|
||||
as: 'company',
|
||||
foreignKey: {
|
||||
name: 'companyId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.deals, {
|
||||
as: 'deal',
|
||||
foreignKey: {
|
||||
name: 'dealId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.project_templates, {
|
||||
as: 'template',
|
||||
foreignKey: {
|
||||
name: 'templateId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.service_lines, {
|
||||
as: 'service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.users, {
|
||||
as: 'project_manager',
|
||||
foreignKey: {
|
||||
name: 'project_managerId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.projects.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.projects.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return projects;
|
||||
};
|
||||
|
||||
|
||||
155
backend/src/db/models/public_holidays.js
Normal file
155
backend/src/db/models/public_holidays.js
Normal file
@ -0,0 +1,155 @@
|
||||
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 public_holidays = sequelize.define(
|
||||
'public_holidays',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
country: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
is_recurring_yearly: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
public_holidays.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.public_holidays.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.public_holidays.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.public_holidays.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.public_holidays.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return public_holidays;
|
||||
};
|
||||
|
||||
|
||||
177
backend/src/db/models/resource_allocations.js
Normal file
177
backend/src/db/models/resource_allocations.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 resource_allocations = sequelize.define(
|
||||
'resource_allocations',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
start_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
end_at: {
|
||||
type: DataTypes.DATE,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
allocated_hours: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
allocation_type: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"billable",
|
||||
|
||||
|
||||
"non_billable"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
resource_allocations.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.resource_allocations.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.resource_allocations.belongsTo(db.projects, {
|
||||
as: 'project',
|
||||
foreignKey: {
|
||||
name: 'projectId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.resource_allocations.belongsTo(db.team_members, {
|
||||
as: 'team_member',
|
||||
foreignKey: {
|
||||
name: 'team_memberId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.resource_allocations.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.resource_allocations.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.resource_allocations.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return resource_allocations;
|
||||
};
|
||||
|
||||
|
||||
151
backend/src/db/models/roles.js
Normal file
151
backend/src/db/models/roles.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 roles = sequelize.define(
|
||||
'roles',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
role_customization: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
globalAccess: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
282
backend/src/db/models/service_lines.js
Normal file
282
backend/src/db/models/service_lines.js
Normal file
@ -0,0 +1,282 @@
|
||||
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 service_lines = sequelize.define(
|
||||
'service_lines',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
name_en: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description_en: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
category: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"ai_ml",
|
||||
|
||||
|
||||
"cybersecurity",
|
||||
|
||||
|
||||
"cloud",
|
||||
|
||||
|
||||
"hardware",
|
||||
|
||||
|
||||
"web_mobile",
|
||||
|
||||
|
||||
"database"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
pricing_model: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"project_fixed",
|
||||
|
||||
|
||||
"time_material",
|
||||
|
||||
|
||||
"recurring",
|
||||
|
||||
|
||||
"hybrid"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
min_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
max_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
typical_timeline: {
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
service_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.service_lines.hasMany(db.services, {
|
||||
as: 'services_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.deals, {
|
||||
as: 'deals_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.project_templates, {
|
||||
as: 'project_templates_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.projects, {
|
||||
as: 'projects_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.time_entries, {
|
||||
as: 'time_entries_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.contracts, {
|
||||
as: 'contracts_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.service_lines.hasMany(db.support_tickets, {
|
||||
as: 'support_tickets_service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.service_lines.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.service_lines.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.service_lines.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.service_lines.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return service_lines;
|
||||
};
|
||||
|
||||
|
||||
228
backend/src/db/models/services.js
Normal file
228
backend/src/db/models/services.js
Normal file
@ -0,0 +1,228 @@
|
||||
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 services = sequelize.define(
|
||||
'services',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
name_en: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description_fr: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
description_en: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
deliverables: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
pricing_model: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"project_fixed",
|
||||
|
||||
|
||||
"time_material",
|
||||
|
||||
|
||||
"recurring",
|
||||
|
||||
|
||||
"hybrid"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
min_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
max_price: {
|
||||
type: DataTypes.DECIMAL,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
typical_duration_weeks: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
required_skills: {
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
services.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.services.hasMany(db.leads, {
|
||||
as: 'leads_service_interest',
|
||||
foreignKey: {
|
||||
name: 'service_interestId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.services.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.services.belongsTo(db.service_lines, {
|
||||
as: 'service_line',
|
||||
foreignKey: {
|
||||
name: 'service_lineId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.services.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.services.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.services.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return services;
|
||||
};
|
||||
|
||||
|
||||
163
backend/src/db/models/skills.js
Normal file
163
backend/src/db/models/skills.js
Normal file
@ -0,0 +1,163 @@
|
||||
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 skills = sequelize.define(
|
||||
'skills',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
domain: {
|
||||
type: DataTypes.ENUM,
|
||||
|
||||
|
||||
|
||||
values: [
|
||||
|
||||
"ai_ml",
|
||||
|
||||
|
||||
"cybersecurity",
|
||||
|
||||
|
||||
"cloud",
|
||||
|
||||
|
||||
"hardware",
|
||||
|
||||
|
||||
"web_mobile",
|
||||
|
||||
|
||||
"database",
|
||||
|
||||
|
||||
"general"
|
||||
|
||||
],
|
||||
|
||||
},
|
||||
|
||||
importHash: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
paranoid: true,
|
||||
freezeTableName: true,
|
||||
},
|
||||
);
|
||||
|
||||
skills.associate = (db) => {
|
||||
|
||||
|
||||
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.skills.hasMany(db.team_member_skills, {
|
||||
as: 'team_member_skills_skill',
|
||||
foreignKey: {
|
||||
name: 'skillId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//end loop
|
||||
|
||||
|
||||
|
||||
db.skills.belongsTo(db.tenants, {
|
||||
as: 'tenant',
|
||||
foreignKey: {
|
||||
name: 'tenantId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
db.skills.belongsTo(db.organizations, {
|
||||
as: 'organizations',
|
||||
foreignKey: {
|
||||
name: 'organizationsId',
|
||||
},
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
db.skills.belongsTo(db.users, {
|
||||
as: 'createdBy',
|
||||
});
|
||||
|
||||
db.skills.belongsTo(db.users, {
|
||||
as: 'updatedBy',
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return skills;
|
||||
};
|
||||
|
||||
|
||||
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