@7365admin1/core 2.82.0 → 2.83.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -29991,12 +29991,91 @@ function useCustomerSiteRepo() {
29991
29991
  } catch (error) {
29992
29992
  }
29993
29993
  }
29994
+ async function getById(id) {
29995
+ try {
29996
+ id = new ObjectId54(id);
29997
+ } catch {
29998
+ throw new BadRequestError90("Invalid ID.");
29999
+ }
30000
+ const data = await collection.findOne({
30001
+ _id: id
30002
+ });
30003
+ if (!data) {
30004
+ throw new BadRequestError90("Customer site not found.");
30005
+ }
30006
+ return data;
30007
+ }
30008
+ async function updateCustomerSiteById(id, payload, session) {
30009
+ try {
30010
+ id = new ObjectId54(id);
30011
+ } catch {
30012
+ throw new BadRequestError90("Invalid ID.");
30013
+ }
30014
+ delete payload._id;
30015
+ const result = await collection.findOneAndUpdate(
30016
+ { _id: id, status: "active" },
30017
+ {
30018
+ $set: {
30019
+ ...payload,
30020
+ updatedAt: /* @__PURE__ */ new Date()
30021
+ }
30022
+ },
30023
+ {
30024
+ returnDocument: "after",
30025
+ session
30026
+ }
30027
+ );
30028
+ if (!result) {
30029
+ throw new BadRequestError90("Customer site not found.");
30030
+ }
30031
+ delNamespace().then(() => {
30032
+ logger70.info(`Cache cleared for namespace: ${namespace_collection}`);
30033
+ }).catch((err) => {
30034
+ logger70.error(`Failed to clear cache for namespace: ${namespace_collection}`, err);
30035
+ });
30036
+ return result;
30037
+ }
30038
+ async function deleteById(id, session) {
30039
+ try {
30040
+ id = new ObjectId54(id);
30041
+ } catch {
30042
+ throw new BadRequestError90("Invalid ID.");
30043
+ }
30044
+ const result = await collection.findOneAndUpdate(
30045
+ {
30046
+ _id: id,
30047
+ status: { $ne: "deleted" }
30048
+ },
30049
+ {
30050
+ $set: {
30051
+ status: "deleted",
30052
+ deletedAt: /* @__PURE__ */ new Date()
30053
+ }
30054
+ },
30055
+ {
30056
+ returnDocument: "after",
30057
+ session
30058
+ }
30059
+ );
30060
+ if (!result) {
30061
+ throw new BadRequestError90("Customer site not found.");
30062
+ }
30063
+ delNamespace().then(() => {
30064
+ logger70.info(`Cache cleared for namespace: ${namespace_collection}`);
30065
+ }).catch((err) => {
30066
+ logger70.error(`Failed to clear cache for namespace: ${namespace_collection}`, err);
30067
+ });
30068
+ return result;
30069
+ }
29994
30070
  return {
29995
30071
  createIndexes,
29996
30072
  add,
29997
30073
  getAll,
29998
30074
  getByName,
29999
- getBySiteAsServiceProvider
30075
+ getBySiteAsServiceProvider,
30076
+ getById,
30077
+ updateCustomerSiteById,
30078
+ deleteById
30000
30079
  };
30001
30080
  }
30002
30081
 
@@ -30008,12 +30087,13 @@ import {
30008
30087
  } from "@7365admin1/node-server-utils";
30009
30088
  import levenshtein from "fast-levenshtein";
30010
30089
  function useCustomerSiteService() {
30011
- const { add: _add } = useCustomerSiteRepo();
30090
+ const { add: _add, updateCustomerSiteById, getById: _getById } = useCustomerSiteRepo();
30012
30091
  const {
30013
30092
  getByName: getSiteByName,
30014
30093
  getByExactName: getSiteByExactName,
30015
30094
  createSite,
30016
- getSiteById
30095
+ getSiteById,
30096
+ updateSiteById
30017
30097
  } = useSiteRepo();
30018
30098
  const {
30019
30099
  getById: _getByOrgId,
@@ -30144,9 +30224,47 @@ function useCustomerSiteService() {
30144
30224
  session?.endSession();
30145
30225
  }
30146
30226
  }
30227
+ async function updateCusSiteById(customerSiteId, payload) {
30228
+ const session = useAtlas44.getClient()?.startSession();
30229
+ session?.startTransaction();
30230
+ try {
30231
+ const customerSite = await _getById(customerSiteId);
30232
+ if (!customerSite) {
30233
+ throw new NotFoundError20("Customer site not found.");
30234
+ }
30235
+ const siteId = customerSite.site;
30236
+ await updateSiteById(
30237
+ siteId,
30238
+ {
30239
+ ...payload.name !== void 0 && { name: payload.name },
30240
+ ...payload.address !== void 0 && { address: payload.address },
30241
+ ...payload.category !== void 0 && { category: payload.category }
30242
+ },
30243
+ session
30244
+ );
30245
+ const safePayload = {
30246
+ ...payload.name !== void 0 && { name: payload.name },
30247
+ ...payload.address !== void 0 && { address: payload.address },
30248
+ ...payload.category !== void 0 && { category: payload.category }
30249
+ };
30250
+ const result = await updateCustomerSiteById(
30251
+ customerSiteId,
30252
+ safePayload,
30253
+ session
30254
+ );
30255
+ await session?.commitTransaction();
30256
+ return result;
30257
+ } catch (error) {
30258
+ await session?.abortTransaction();
30259
+ throw error;
30260
+ } finally {
30261
+ await session?.endSession();
30262
+ }
30263
+ }
30147
30264
  return {
30148
30265
  add,
30149
- addViaInvite
30266
+ addViaInvite,
30267
+ updateCusSiteById
30150
30268
  };
30151
30269
  }
30152
30270
 
@@ -30156,9 +30274,11 @@ import { BadRequestError as BadRequestError92, logger as logger71 } from "@7365a
30156
30274
  function useCustomerSiteController() {
30157
30275
  const {
30158
30276
  getAll: _getAll,
30159
- getBySiteAsServiceProvider: _getBySiteAsServiceProvider
30277
+ getBySiteAsServiceProvider: _getBySiteAsServiceProvider,
30278
+ getById: _getById,
30279
+ deleteById: _deleteById
30160
30280
  } = useCustomerSiteRepo();
30161
- const { add: _add, addViaInvite: _addViaInvite } = useCustomerSiteService();
30281
+ const { add: _add, addViaInvite: _addViaInvite, updateCusSiteById: _updateCusSiteById } = useCustomerSiteService();
30162
30282
  async function add(req, res, next) {
30163
30283
  const { error, value } = schemaCustomerSite.validate(req.body);
30164
30284
  if (error) {
@@ -30275,11 +30395,63 @@ function useCustomerSiteController() {
30275
30395
  return;
30276
30396
  }
30277
30397
  }
30398
+ async function getById(req, res, next) {
30399
+ const validation = Joi52.object({
30400
+ id: Joi52.string().hex().required()
30401
+ });
30402
+ const id = req.params.id;
30403
+ const { error } = validation.validate({ id });
30404
+ if (error) {
30405
+ next(new BadRequestError92(error.message));
30406
+ return;
30407
+ }
30408
+ try {
30409
+ const data = await _getById(id);
30410
+ res.json(data);
30411
+ return;
30412
+ } catch (error2) {
30413
+ next(error2);
30414
+ return;
30415
+ }
30416
+ }
30417
+ async function updateById(req, res, next) {
30418
+ const validation = Joi52.object({
30419
+ id: Joi52.string().hex().required()
30420
+ });
30421
+ const id = req.params.id;
30422
+ const { error } = validation.validate({ id });
30423
+ if (error) {
30424
+ next(new BadRequestError92(error.message));
30425
+ return;
30426
+ }
30427
+ try {
30428
+ const data = await _updateCusSiteById(id, req.body);
30429
+ res.json(data);
30430
+ return;
30431
+ } catch (error2) {
30432
+ next(error2);
30433
+ return;
30434
+ }
30435
+ }
30436
+ async function deleteById(req, res, next) {
30437
+ const id = req.params.id;
30438
+ try {
30439
+ const data = await _deleteById(id);
30440
+ res.json(data);
30441
+ return;
30442
+ } catch (error) {
30443
+ next(error);
30444
+ return;
30445
+ }
30446
+ }
30278
30447
  return {
30279
30448
  add,
30280
30449
  getAll,
30281
30450
  addViaInvite,
30282
- getBySiteAsServiceProvider
30451
+ getBySiteAsServiceProvider,
30452
+ getById,
30453
+ updateById,
30454
+ deleteById
30283
30455
  };
30284
30456
  }
30285
30457