summaryrefslogtreecommitdiff
path: root/src/app/db/service/base/many.js
blob: 1791694cffed78ed97da3796ac3fda2befc076ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * Service API methods that affect multiple records
 * @module app/db/service/base/methods
 */

import * as db from "app/db/query";
import { reduceValidColumns } from "app/db/helpers";
import { PERMISSIONS } from "app/constants";
import debugModule from "debug";

/**
 * Debug logger
 */
const debug = debugModule("shoebox:service");

/**
 * API to query for multiple records by ID
 */
export function showMany(service) {
  const { Model, parent, resource } = service;
  const { childRelation } = service.options;
  const idAttribute = service.idAttributes[0];
  return async function showManyMiddleware(request, response, next) {
    const { user, permission, body } = request;
    const ids = body.map((item) => item[idAttribute]).filter((id) => !!id);
    let data;
    try {
      if (parent) {
        // Fetch the immediate parent of the pivot table based on the name of the parent resource.
        // This instance is added to the `request.parents` object when performing the permissions check.
        const parentInstance = request.parents[parent.resource];
        data = await parentInstance
          .related(childRelation)
          .query((builder) => builder.whereIn(idAttribute, ids));
      } else {
        data = await db.showIDs({
          Model,
          ids,
        });
      }
    } catch (error) {
      debug(`${resource} Show error`);
      debug(error);
      return next(error);
    }
    if (!data) {
      response.locals = { data: [] };
      next();
    } else if (
      permission === PERMISSIONS.ALLOW_FOR_OWNER &&
      data.some((item) => item.get("user_id") !== user.user_id)
    ) {
      next(new Error("PermissionsError"));
    } else {
      response.locals = { data };
      next();
    }
  };
}

/**
 * API to update multiple records
 */
export function updateMany(service) {
  return async function updateManyMiddleware(request, response, next) {
    const data = await handleUpdateManyWithTransaction(
      service,
      request,
      response
    );
    response.locals.data = data;
    next();
  };
}

/**
 * Update multiple records using a transaction
 */
export async function handleUpdateManyWithTransaction(
  service,
  request,
  response
) {
  const { Model, idAttributes, columns } = service;
  const { bookshelf, privateFields } = service.options;
  const idAttribute = idAttributes[0];
  return await bookshelf.transaction((transaction) => {
    const { data: instances } = response.locals;
    const { params, user, permission, body } = request;
    const instanceLookup = instances.reduce((lookup, instance) => {
      lookup[instance.id] = instance;
      return lookup;
    }, {});
    const promises = body.map((item) => {
      const itemId = item[idAttribute];
      if (!itemId) {
        return handleCreate({
          Model,
          body: item,
          idAttributes,
          params,
          columns,
          privateFields,
          transaction,
        });
      } else if (itemId in instanceLookup) {
        return handleUpdate({
          instance: instanceLookup[itemId],
          body: item,
          user,
          permission,
          columns,
          privateFields,
          transaction,
        });
      } else {
        throw new Error("item id not found on this record");
      }
    });
    return Promise.all(promises);
  });
}

/**
 * API to destroy multiple records
 */
export function destroyMany(service) {
  const { Model, idAttributes } = service;
  const [idAttribute, ...parentIdAttributes] = idAttributes;
  return async function destroyManyMiddleware(request, response, next) {
    const idsToDelete = request.body[idAttribute];
    let instances;
    try {
      instances = await db.showIDs({ Model, ids: idsToDelete });
      instances.forEach((instance) => {
        parentIdAttributes.forEach((parentIdAttribute) => {
          if (
            instance.get(parentIdAttribute) !==
            request.params[parentIdAttribute]
          ) {
            throw new Error("parent mismatch");
          }
        });
      });
      await Promise.all(instances.map((instance) => instance.destroy()));
    } catch (error) {
      debug(`${service.resource} destroy many error`);
      debug(error);
      return next(new Error(error));
    }
    response.locals.data = instances;
    response.locals.success = true;
    response.locals.id = idsToDelete;
    next();
  };
}

/**
 * Insert a single record
 */
export function handleCreate({
  idAttributes,
  params,
  columns,
  body,
  Model,
  privateFields,
  transaction,
}) {
  body = reduceValidColumns(body, columns, privateFields);
  if (idAttributes) {
    idAttributes.forEach((idAttribute) => {
      if (idAttribute in params && idAttribute in columns) {
        body[idAttribute] = parseInt(params[idAttribute]);
      }
    });
  }
  return db.create({ Model: Model, data: body, transaction });
}

/**
 * Update a single record
 */
export function handleUpdate({
  instance,
  body,
  user,
  permission,
  columns,
  privateFields,
  transaction,
}) {
  if (
    permission === PERMISSIONS.ALLOW_FOR_OWNER &&
    instance.get("user_id") !== user.user_id
  ) {
    throw new Error("PermissionsError");
  }
  body = reduceValidColumns(body, columns, privateFields);
  return db.update({
    instance,
    data: body,
    transaction,
  });
}