• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

restorecommerce / resource-srv / 6259571471

21 Sep 2023 09:07AM UTC coverage: 51.634% (+0.06%) from 51.571%
6259571471

push

github

Arun-KumarH
chore: Release v1.1.0 - See CHANGELOG

87 of 245 branches covered (0.0%)

Branch coverage included in aggregate %.

26 of 26 new or added lines in 3 files covered. (100.0%)

308 of 520 relevant lines covered (59.23%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.9
/src/commandInterface.ts
1
import * as _ from 'lodash';
1✔
2
import { RedisClientType } from 'redis';
3
import {
1✔
4
  CommandInterface, Server, GraphDatabaseProvider
5
} from '@restorecommerce/chassis-srv';
6
import { Events } from '@restorecommerce/kafka-client';
7

8
export class ResourceCommandInterface extends CommandInterface {
1✔
9
  edgeCfg: any;
10
  // graphName: any;
11
  constructor(server: Server, cfg: any, logger: any, events: Events, redisClient: RedisClientType<any, any>) {
12
    super(server, cfg, logger, events, redisClient);
1✔
13
    let graphCfg = cfg.get('graph');
1✔
14
    if (graphCfg && graphCfg.vertices) {
1!
15
      this.edgeCfg = graphCfg.vertices;
1✔
16
      // this.graphName = cfg.graph.graphName;
17
    }
18
  }
19

20
  makeResourcesRestoreSetup(db: GraphDatabaseProvider, resource: string): any {
21
    const that = this;
×
22
    const collectionName = `${resource}s`;
×
23
    return {
×
24
      [`${resource}Created`]: async function restoreCreated(message: any, context: any,
25
        config: any, eventName: string): Promise<any> {
26
        that.decodeBufferField(message, resource);
×
27
        if (that.edgeCfg[collectionName]) {
×
28
          const result = await db.findByID(collectionName, message.id);
×
29
          if (result?.length > 0) {
×
30
            return {};
×
31
          }
32
          await db.createVertex(collectionName, message);
×
33
          // Based on graphCfg create the necessary edges
34
          for (let eachEdgeCfg of that.edgeCfg[collectionName]) {
×
35
            const fromIDkey = eachEdgeCfg?.from;
×
36
            const from_id = message[fromIDkey];
×
37
            const toIDkey = eachEdgeCfg?.to;
×
38
            const to_id = message[toIDkey];
×
39
            const fromVerticeName = collectionName;
×
40
            const toVerticeName = eachEdgeCfg?.toVerticeName;
×
41
            if (fromVerticeName && toVerticeName) {
×
42
              await db.addEdgeDefinition(eachEdgeCfg.edgeName, [fromVerticeName],
×
43
                [toVerticeName]);
44
            }
45
            if (from_id && to_id) {
×
46
              if (_.isArray(to_id)) {
×
47
                for (let toID of to_id) {
×
48
                  await db.createEdge(eachEdgeCfg.edgeName, null,
×
49
                    `${fromVerticeName}/${from_id}`, `${toVerticeName}/${toID}`);
50
                }
51
                continue;
×
52
              }
53
              await db.createEdge(eachEdgeCfg.edgeName, null,
×
54
                `${fromVerticeName}/${from_id}`, `${toVerticeName}/${to_id}`);
55
            }
56
          }
57
        } else {
58
          await db.insert(collectionName, message);
×
59
        }
60
        return {};
×
61
      },
62
      [`${resource}Modified`]: async function restoreModified(message: any, context: any, config: any,
63
        eventName: string): Promise<any> {
64
        that.decodeBufferField(message, resource);
×
65
        // Based on graphcfg update necessary edges
66
        if (that.edgeCfg[collectionName]) {
×
67
          const foundDocs = await db.find(collectionName, { id: message.id });
×
68
          const dbDoc = foundDocs[0];
×
69
          for (let eachEdgeCfg of that.edgeCfg[collectionName]) {
×
70
            const toIDkey = eachEdgeCfg?.to;
×
71
            let modified_to_idValues = message[toIDkey];
×
72
            let db_to_idValues = dbDoc[toIDkey];
×
73
            if (_.isArray(modified_to_idValues)) {
×
74
              modified_to_idValues = _.sortBy(modified_to_idValues);
×
75
            }
76
            if (_.isArray(db_to_idValues)) {
×
77
              db_to_idValues = _.sortBy(db_to_idValues);
×
78
            }
79
            // delete and recreate only if there is a difference in references
80
            if (!_.isEqual(modified_to_idValues, db_to_idValues)) {
×
81
              const fromIDkey = eachEdgeCfg?.from;
×
82
              const from_id = message[fromIDkey];
×
83
              const fromVerticeName = collectionName;
×
84
              const toVerticeName = eachEdgeCfg?.toVerticeName;
×
85

86
              const edgeCollectionName = eachEdgeCfg?.edgeName;
×
87
              let outgoingEdges: any = await db.getOutEdges(edgeCollectionName, `${collectionName}/${dbDoc.id}`);
×
88
              for (let outgoingEdge of outgoingEdges) {
×
89
                const removedEdge = await db.removeEdge(edgeCollectionName, outgoingEdge._id);
×
90
              }
91
              // Create new edges
92
              if (from_id && modified_to_idValues) {
×
93
                if (_.isArray(modified_to_idValues)) {
×
94
                  for (let toID of modified_to_idValues) {
×
95
                    await db.createEdge(eachEdgeCfg?.edgeName, null,
×
96
                      `${fromVerticeName}/${from_id}`, `${toVerticeName}/${toID}`);
97
                  }
98
                  continue;
×
99
                }
100
                await db.createEdge(edgeCollectionName, null,
×
101
                  `${fromVerticeName}/${from_id}`, `${toVerticeName}/${modified_to_idValues}`);
102
              }
103
            }
104
          }
105
        }
106
        message = _.omitBy(message, _.isNil);
×
107
        await db.update(collectionName, message);
×
108
        return {};
×
109
      },
110
      [`${resource}Deleted`]: async function restoreDeleted(message: any, context: any, config: any,
111
        eventName: string): Promise<any> {
112
        if (that.edgeCfg[collectionName]) {
×
113
          // Modify the Ids to include documentHandle
114
          await db.removeVertex(collectionName, `${collectionName}/${message.id}`);
×
115
        } else {
116
          await db.delete(collectionName, [message.id]);
×
117
        }
118
        return {};
×
119
      }
120
    };
121
  }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc