abhishek-akbari01 commited on
Commit
6150c79
·
1 Parent(s): 441c9e3

create workorders migration with sync api

Browse files
src/controllers/propertyware/workorders.controller.ts CHANGED
@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
2
 
3
  import { fetchAllWorkorders } from '../../shared/services/propertyware.service';
4
  import { logger } from '../../utils/logger';
 
5
 
6
  export const fetchWorkorders = async (req: Request, res: Response) => {
7
  try {
@@ -28,3 +29,45 @@ export const fetchWorkorders = async (req: Request, res: Response) => {
28
  res.status(500).json({ error: 'Error fetching Workorders' });
29
  }
30
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import { fetchAllWorkorders } from '../../shared/services/propertyware.service';
4
  import { logger } from '../../utils/logger';
5
+ import PwWorkOrders from '../../models/pwWorkOrders';
6
 
7
  export const fetchWorkorders = async (req: Request, res: Response) => {
8
  try {
 
29
  res.status(500).json({ error: 'Error fetching Workorders' });
30
  }
31
  };
32
+
33
+
34
+ export const syncWorkOrders = async (req: Request, res: Response) => {
35
+ try {
36
+ const workOrderData = await fetchAllWorkorders();
37
+
38
+ const workOrderResponseData = workOrderData.map((workOrder: {
39
+ id: number;
40
+ number: number;
41
+ location: string;
42
+ portfolioID: number;
43
+ buildingID: number;
44
+ unitID: number;
45
+ status: string;
46
+ assignedVendors: string[];
47
+ }) => ({
48
+ pw_id: workOrder.id,
49
+ number: workOrder.number,
50
+ location: workOrder.location,
51
+ portfolio_id: workOrder.portfolioID,
52
+ building_id: workOrder.buildingID,
53
+ unit_id: workOrder.unitID,
54
+ status: workOrder.status,
55
+ assignedVendors: workOrder.assignedVendors || [],
56
+ }));
57
+
58
+ try {
59
+ const createResult = await PwWorkOrders.bulkCreate(workOrderResponseData, {
60
+ updateOnDuplicate: ['number', 'location', 'portfolio_id', 'building_id', 'unit_id', 'status', 'assigned_vendors'],
61
+ });
62
+
63
+ res.end("Work orders synced successfully");
64
+ } catch (error) {
65
+ logger.error("Error inserting Work Orders: ", error);
66
+ res.status(500).end("Error inserting Work Orders");
67
+ }
68
+
69
+ } catch (error) {
70
+ logger.error("Error fetching Work Orders: ", error);
71
+ res.status(500).end("Error fetching Work Orders");
72
+ }
73
+ };
src/db/migrations/20240927063731-create-pw-workorders-table.js ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use strict';
2
+
3
+ /** @type {import('sequelize-cli').Migration} */
4
+ module.exports = {
5
+ async up(queryInterface, Sequelize) {
6
+ await queryInterface.createTable('pw_workorders', {
7
+ id: {
8
+ allowNull: false,
9
+ autoIncrement: true,
10
+ primaryKey: true,
11
+ type: Sequelize.INTEGER,
12
+ },
13
+ pw_id: {
14
+ allowNull: false,
15
+ type: Sequelize.BIGINT,
16
+ },
17
+ number: {
18
+ allowNull: false,
19
+ type: Sequelize.INTEGER,
20
+ },
21
+ location: {
22
+ allowNull: false,
23
+ type: Sequelize.STRING,
24
+ },
25
+ portfolio_id: {
26
+ allowNull: false,
27
+ type: Sequelize.BIGINT,
28
+ },
29
+ building_id: {
30
+ allowNull: false,
31
+ type: Sequelize.BIGINT,
32
+ },
33
+ unit_id: {
34
+ allowNull: false,
35
+ type: Sequelize.BIGINT,
36
+ },
37
+ status: {
38
+ allowNull: false,
39
+ type: Sequelize.STRING,
40
+ },
41
+ assigned_vendors: {
42
+ allowNull: true,
43
+ type: Sequelize.JSON,
44
+ },
45
+ created_at: {
46
+ allowNull: false,
47
+ type: Sequelize.DATE,
48
+ },
49
+ updated_at: {
50
+ allowNull: false,
51
+ type: Sequelize.DATE,
52
+ },
53
+ });
54
+ },
55
+
56
+ async down(queryInterface, Sequelize) {
57
+ await queryInterface.dropTable('pw_workorders');
58
+ }
59
+ };
src/models/pwWorkOrders.ts ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ DataTypes,
3
+ Model,
4
+ CreationOptional
5
+ } from 'sequelize';
6
+ import { sequelize } from './index';
7
+ import { PwWorkOrdersInterface } from '../shared/interfaces/pwWorkOrdersInterface';
8
+
9
+ class PwWorkOrders extends Model<PwWorkOrdersInterface> implements PwWorkOrdersInterface
10
+ {
11
+ declare id: CreationOptional<number>;
12
+ declare pw_id: number;
13
+ declare number: number;
14
+ declare location: string;
15
+ declare portfolio_id: number;
16
+ declare building_id: number;
17
+ declare unit_id: number;
18
+ declare status: string;
19
+ declare assigned_vendors: string[];
20
+ }
21
+
22
+ PwWorkOrders.init(
23
+ {
24
+ id: {
25
+ type: DataTypes.BIGINT.UNSIGNED,
26
+ autoIncrement: true,
27
+ primaryKey: true,
28
+ unique: true,
29
+ },
30
+ pw_id: {
31
+ type: DataTypes.BIGINT,
32
+ allowNull: false,
33
+ },
34
+ number: {
35
+ type: DataTypes.BIGINT,
36
+ allowNull: false,
37
+ },
38
+ location: {
39
+ type: DataTypes.STRING,
40
+ allowNull: false,
41
+ },
42
+ portfolio_id: {
43
+ type: DataTypes.BIGINT,
44
+ allowNull: false,
45
+ },
46
+ building_id: {
47
+ type: DataTypes.BIGINT,
48
+ allowNull: false,
49
+ },
50
+ unit_id: {
51
+ type: DataTypes.BIGINT,
52
+ allowNull: false,
53
+ },
54
+ status: {
55
+ type: DataTypes.STRING,
56
+ allowNull: false,
57
+ },
58
+ assigned_vendors: {
59
+ type: DataTypes.JSON,
60
+ allowNull: true,
61
+ },
62
+ },
63
+ {
64
+ sequelize,
65
+ tableName: 'pw_workorders',
66
+ underscored: true,
67
+ freezeTableName: true,
68
+ timestamps: true,
69
+ createdAt: 'created_at',
70
+ updatedAt: 'updated_at',
71
+ }
72
+ );
73
+
74
+ export default PwWorkOrders;
src/routes/propertyware.routes.ts CHANGED
@@ -7,7 +7,7 @@ import { LocationLookup, syncUnitsData } from "../controllers/propertyware/units
7
  import { fetchVendorsData } from "../controllers/propertyware/vendors.controller";
8
  import { syncInvoices } from "../controllers/propertyware/bills.controller";
9
  import { jwtMiddleware } from '../middlewares/authMiddleware';
10
- import { fetchWorkorders } from "../controllers/propertyware/workorders.controller";
11
 
12
  const pwRouter = express.Router();
13
 
@@ -68,6 +68,20 @@ pwRouter.get("/syncUnits", syncUnitsData);
68
  */
69
  pwRouter.get('/syncInvoices', syncInvoices);
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  /**
73
  * @swagger
 
7
  import { fetchVendorsData } from "../controllers/propertyware/vendors.controller";
8
  import { syncInvoices } from "../controllers/propertyware/bills.controller";
9
  import { jwtMiddleware } from '../middlewares/authMiddleware';
10
+ import { fetchWorkorders, syncWorkOrders } from "../controllers/propertyware/workorders.controller";
11
 
12
  const pwRouter = express.Router();
13
 
 
68
  */
69
  pwRouter.get('/syncInvoices', syncInvoices);
70
 
71
+ /**
72
+ * @swagger
73
+ * /api/pw/syncWorkOrders:
74
+ * get:
75
+ * summary: Sync WorkOrders data
76
+ * tags: [Propertyware]
77
+ * responses:
78
+ * 200:
79
+ * description: WorkOrders data synced successfully
80
+ * 500:
81
+ * description: Error syncing workOrders data
82
+ */
83
+ pwRouter.get("/syncWorkOrders", syncWorkOrders);
84
+
85
 
86
  /**
87
  * @swagger
src/shared/interfaces/pwWorkOrdersInterface.ts ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export interface PwWorkOrdersInterface {
2
+ id: number;
3
+ pw_id: number;
4
+ number: number;
5
+ location: string;
6
+ portfolio_id: number;
7
+ building_id: number;
8
+ unit_id: number;
9
+ status: string;
10
+ assigned_vendors: string[];
11
+ }