@7365admin1/core 2.63.0 → 2.64.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
@@ -53708,6 +53708,370 @@ function useRoleControllerV2() {
53708
53708
  createRole
53709
53709
  };
53710
53710
  }
53711
+
53712
+ // src/models/post-preloved.model.ts
53713
+ import Joi132 from "joi";
53714
+ import { ObjectId as ObjectId129 } from "mongodb";
53715
+ var PostStatus = /* @__PURE__ */ ((PostStatus2) => {
53716
+ PostStatus2["PUBLISHED"] = "published";
53717
+ PostStatus2["SOLD"] = "sold";
53718
+ PostStatus2["DELETED"] = "deleted";
53719
+ return PostStatus2;
53720
+ })(PostStatus || {});
53721
+ var PostSort = /* @__PURE__ */ ((PostSort2) => {
53722
+ PostSort2["CREATED_AT"] = "createdAt";
53723
+ PostSort2["TITLE"] = "title";
53724
+ PostSort2["PRICE"] = "price";
53725
+ PostSort2["ID"] = "_id";
53726
+ return PostSort2;
53727
+ })(PostSort || {});
53728
+ var PostOrder = /* @__PURE__ */ ((PostOrder2) => {
53729
+ PostOrder2["ASC"] = "asc";
53730
+ PostOrder2["DESC"] = "desc";
53731
+ return PostOrder2;
53732
+ })(PostOrder || {});
53733
+ var schemaPost = Joi132.object({
53734
+ _id: Joi132.string().hex().optional().allow("", null),
53735
+ title: Joi132.string().required(),
53736
+ description: Joi132.string().optional().allow("", null),
53737
+ attachments: Joi132.array().items(Joi132.string().hex().optional()).optional().allow(null),
53738
+ category: Joi132.array().items(Joi132.string().hex().optional()).optional().allow(null),
53739
+ currency: Joi132.string().optional().allow("", null),
53740
+ price: Joi132.number().required(),
53741
+ status: Joi132.string().valid(...Object.values(PostStatus)).optional().default("published" /* PUBLISHED */),
53742
+ reserverId: Joi132.string().hex().optional().allow("", null),
53743
+ createdBy: Joi132.string().hex().optional().allow("", null),
53744
+ site: Joi132.string().hex().optional().allow("", null),
53745
+ createdAt: Joi132.date().optional().allow(null),
53746
+ updatedAt: Joi132.date().optional().allow(null),
53747
+ deletedAt: Joi132.date().optional().allow(null)
53748
+ });
53749
+ function MPost(value) {
53750
+ const { error } = schemaPost.validate(value);
53751
+ if (error) {
53752
+ throw new Error(error.details[0].message);
53753
+ }
53754
+ if (value._id && typeof value._id === "string") {
53755
+ try {
53756
+ value._id = new ObjectId129(value._id);
53757
+ } catch {
53758
+ throw new Error("Invalid ID.");
53759
+ }
53760
+ }
53761
+ if (value.site && typeof value.site === "string") {
53762
+ try {
53763
+ value.site = new ObjectId129(value.site);
53764
+ } catch {
53765
+ throw new Error("Invalid site ID.");
53766
+ }
53767
+ }
53768
+ if (value.createdBy && typeof value.createdBy === "string") {
53769
+ try {
53770
+ value.createdBy = new ObjectId129(value.createdBy);
53771
+ } catch {
53772
+ throw new Error("Invalid createdBy ID.");
53773
+ }
53774
+ }
53775
+ if (value.reserverId && typeof value.reserverId === "string") {
53776
+ try {
53777
+ value.reserverId = new ObjectId129(value.reserverId);
53778
+ } catch {
53779
+ throw new Error("Invalid reserver ID.");
53780
+ }
53781
+ }
53782
+ if (value.attachments && Array.isArray(value.attachments)) {
53783
+ value.attachments = value.attachments.map((id) => {
53784
+ if (typeof id === "string") {
53785
+ try {
53786
+ return new ObjectId129(id);
53787
+ } catch {
53788
+ throw new Error("Invalid attachment ID.");
53789
+ }
53790
+ }
53791
+ return id;
53792
+ });
53793
+ }
53794
+ if (value.category && Array.isArray(value.category)) {
53795
+ value.category = value.category.map((id) => {
53796
+ if (typeof id === "string") {
53797
+ try {
53798
+ return new ObjectId129(id);
53799
+ } catch {
53800
+ throw new Error("Invalid category ID.");
53801
+ }
53802
+ }
53803
+ return id;
53804
+ });
53805
+ }
53806
+ return {
53807
+ _id: value._id ?? new ObjectId129(),
53808
+ title: value.title ?? "",
53809
+ description: value.description ?? "",
53810
+ attachments: value.attachments ?? [],
53811
+ category: value.category ?? [],
53812
+ currency: value.currency ?? "",
53813
+ price: value.price ?? 0,
53814
+ status: value.status ?? "published" /* PUBLISHED */,
53815
+ reserverId: value.reserverId ?? null,
53816
+ createdBy: value.createdBy ?? "",
53817
+ site: value.site ?? "",
53818
+ createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
53819
+ updatedAt: value.updatedAt ?? null,
53820
+ deletedAt: value.deletedAt ?? null
53821
+ };
53822
+ }
53823
+
53824
+ // src/repositories/post-preloved.repo.ts
53825
+ import {
53826
+ BadRequestError as BadRequestError210,
53827
+ InternalServerError as InternalServerError74,
53828
+ NotFoundError as NotFoundError57,
53829
+ paginate as paginate59,
53830
+ useAtlas as useAtlas117
53831
+ } from "@7365admin1/node-server-utils";
53832
+ import { ObjectId as ObjectId130 } from "mongodb";
53833
+ function usePostPrelovedRepo() {
53834
+ const db = useAtlas117.getDb();
53835
+ if (!db) {
53836
+ throw new InternalServerError74("Unable to connect to server.");
53837
+ }
53838
+ const POST_COLLECTION = "post-preloved";
53839
+ const USER_LOOKUP = {
53840
+ $lookup: {
53841
+ from: "users",
53842
+ localField: "createdBy",
53843
+ foreignField: "_id",
53844
+ pipeline: [
53845
+ {
53846
+ $project: {
53847
+ name: 1,
53848
+ profile: 1
53849
+ }
53850
+ }
53851
+ ],
53852
+ as: "createdBy"
53853
+ }
53854
+ };
53855
+ const USER_UNWIND = {
53856
+ $unwind: {
53857
+ path: "$createdBy",
53858
+ preserveNullAndEmptyArrays: true
53859
+ }
53860
+ };
53861
+ const CATEGORY_LOOKUP = {
53862
+ $lookup: {
53863
+ from: "category-preloved",
53864
+ localField: "category",
53865
+ foreignField: "_id",
53866
+ as: "categories"
53867
+ }
53868
+ };
53869
+ const collection = db.collection(POST_COLLECTION);
53870
+ function buildSortObj(filter) {
53871
+ switch (filter) {
53872
+ case "price-low-high":
53873
+ return { price: 1 };
53874
+ case "price-high-low":
53875
+ return { price: -1 };
53876
+ case "recent":
53877
+ default:
53878
+ return { createdAt: -1 };
53879
+ }
53880
+ }
53881
+ async function add(value, session) {
53882
+ try {
53883
+ const doc = MPost(value);
53884
+ const res = await collection.insertOne(doc, { session });
53885
+ return res.insertedId;
53886
+ } catch (error) {
53887
+ const isDuplicated = error.message?.includes("duplicate");
53888
+ if (isDuplicated)
53889
+ throw new BadRequestError210("Post already exists.");
53890
+ throw error;
53891
+ }
53892
+ }
53893
+ async function getById(_id) {
53894
+ try {
53895
+ _id = new ObjectId130(_id);
53896
+ } catch {
53897
+ throw new BadRequestError210("Invalid post ID format.");
53898
+ }
53899
+ try {
53900
+ const result = await collection.aggregate([
53901
+ { $match: { _id, status: { $ne: "deleted" /* DELETED */ } } },
53902
+ USER_LOOKUP,
53903
+ USER_UNWIND,
53904
+ CATEGORY_LOOKUP
53905
+ ]).toArray();
53906
+ const data = result[0] ?? null;
53907
+ if (!data)
53908
+ throw new NotFoundError57("Post not found.");
53909
+ return data;
53910
+ } catch (error) {
53911
+ throw error;
53912
+ }
53913
+ }
53914
+ async function getAll({
53915
+ search = "",
53916
+ page = 1,
53917
+ limit = 10,
53918
+ filter,
53919
+ site,
53920
+ status,
53921
+ category
53922
+ }, session) {
53923
+ page = page > 0 ? page - 1 : 0;
53924
+ if (site) {
53925
+ try {
53926
+ site = new ObjectId130(site);
53927
+ } catch {
53928
+ throw new BadRequestError210("Invalid site ID format.");
53929
+ }
53930
+ }
53931
+ const categoryIds = category && Array.isArray(category) && category.length > 0 ? category.map((id) => {
53932
+ try {
53933
+ return new ObjectId130(id);
53934
+ } catch {
53935
+ throw new BadRequestError210("Invalid category ID format.");
53936
+ }
53937
+ }) : null;
53938
+ const query = {
53939
+ status: { $ne: "deleted" /* DELETED */ },
53940
+ ...site && { site },
53941
+ ...search && {
53942
+ $or: [
53943
+ { title: { $regex: search, $options: "i" } },
53944
+ { description: { $regex: search, $options: "i" } }
53945
+ ]
53946
+ },
53947
+ ...status && {
53948
+ status: { $in: Array.isArray(status) ? status : [status] }
53949
+ },
53950
+ ...categoryIds && { category: { $in: categoryIds } }
53951
+ };
53952
+ const sortObj = buildSortObj(filter);
53953
+ try {
53954
+ const items = await collection.aggregate(
53955
+ [
53956
+ { $match: query },
53957
+ USER_LOOKUP,
53958
+ USER_UNWIND,
53959
+ CATEGORY_LOOKUP,
53960
+ { $sort: sortObj },
53961
+ { $skip: page * limit },
53962
+ { $limit: limit }
53963
+ ],
53964
+ { session }
53965
+ ).toArray();
53966
+ const length = await collection.countDocuments(query, { session });
53967
+ return paginate59(items, page, limit, length);
53968
+ } catch (error) {
53969
+ throw error;
53970
+ }
53971
+ }
53972
+ return {
53973
+ add,
53974
+ getById,
53975
+ getAll
53976
+ };
53977
+ }
53978
+
53979
+ // src/controllers/post-preloved.controller.ts
53980
+ import { BadRequestError as BadRequestError211, logger as logger186 } from "@7365admin1/node-server-utils";
53981
+ import Joi133 from "joi";
53982
+ function usePostPrelovedController() {
53983
+ const {
53984
+ add: _add,
53985
+ getById: _getById,
53986
+ getAll: _getAll
53987
+ } = usePostPrelovedRepo();
53988
+ async function add(req, res, next) {
53989
+ const { error, value } = schemaPost.validate(req.body, {
53990
+ abortEarly: false
53991
+ });
53992
+ if (error) {
53993
+ const messages = error.details.map((d) => d.message).join(", ");
53994
+ logger186.log({ level: "error", message: messages });
53995
+ next(new BadRequestError211(messages));
53996
+ return;
53997
+ }
53998
+ try {
53999
+ const data = await _add(value);
54000
+ res.status(201).json(data);
54001
+ return;
54002
+ } catch (error2) {
54003
+ logger186.log({ level: "error", message: error2.message });
54004
+ next(error2);
54005
+ return;
54006
+ }
54007
+ }
54008
+ async function getById(req, res, next) {
54009
+ const schema2 = Joi133.object({
54010
+ _id: Joi133.string().hex().length(24).required()
54011
+ });
54012
+ const { error, value } = schema2.validate({ _id: req.params.id });
54013
+ if (error) {
54014
+ logger186.log({ level: "error", message: error.message });
54015
+ next(new BadRequestError211(error.message));
54016
+ return;
54017
+ }
54018
+ try {
54019
+ const data = await _getById(value._id);
54020
+ res.status(200).json(data);
54021
+ return;
54022
+ } catch (error2) {
54023
+ logger186.log({ level: "error", message: error2.message });
54024
+ next(error2);
54025
+ return;
54026
+ }
54027
+ }
54028
+ async function getAll(req, res, next) {
54029
+ const validation = Joi133.object({
54030
+ page: Joi133.number().integer().min(1).allow("", null).default(1),
54031
+ limit: Joi133.number().integer().min(1).max(100).allow("", null).default(10),
54032
+ search: Joi133.string().optional().allow("", null),
54033
+ filter: Joi133.string().valid("recent", "price-low-high", "price-high-low").optional().allow("", null),
54034
+ site: Joi133.string().hex().length(24).optional().allow("", null),
54035
+ status: Joi133.alternatives().try(
54036
+ Joi133.array().items(Joi133.string().valid(...Object.values(PostStatus))),
54037
+ Joi133.string().valid(...Object.values(PostStatus))
54038
+ ).optional().allow(null),
54039
+ category: Joi133.alternatives().try(Joi133.array().items(Joi133.string().hex()), Joi133.string().hex()).optional().allow(null)
54040
+ });
54041
+ const { error, value } = validation.validate(req.query, {
54042
+ abortEarly: false
54043
+ });
54044
+ if (error) {
54045
+ const messages = error.details.map((d) => d.message).join(", ");
54046
+ logger186.log({ level: "error", message: messages });
54047
+ next(new BadRequestError211(messages));
54048
+ return;
54049
+ }
54050
+ const { page, limit, search, filter, site, status, category } = value;
54051
+ try {
54052
+ const data = await _getAll({
54053
+ page,
54054
+ limit,
54055
+ search,
54056
+ filter,
54057
+ site,
54058
+ status: status ? Array.isArray(status) ? status : [status] : void 0,
54059
+ category: category ? Array.isArray(category) ? category : [category] : void 0
54060
+ });
54061
+ res.status(200).json(data);
54062
+ return;
54063
+ } catch (error2) {
54064
+ logger186.log({ level: "error", message: error2.message });
54065
+ next(error2);
54066
+ return;
54067
+ }
54068
+ }
54069
+ return {
54070
+ add,
54071
+ getById,
54072
+ getAll
54073
+ };
54074
+ }
53711
54075
  export {
53712
54076
  ANPRMode,
53713
54077
  AccessTypeProps,
@@ -53778,6 +54142,7 @@ export {
53778
54142
  MPatrolQuestion,
53779
54143
  MPatrolRoute,
53780
54144
  MPerson,
54145
+ MPost,
53781
54146
  MPromoCode,
53782
54147
  MRobot,
53783
54148
  MRole,
@@ -53808,6 +54173,9 @@ export {
53808
54173
  Period,
53809
54174
  PersonStatus,
53810
54175
  PersonTypes,
54176
+ PostOrder,
54177
+ PostSort,
54178
+ PostStatus,
53811
54179
  SiteCategories,
53812
54180
  SiteStatus,
53813
54181
  SortFields,
@@ -53903,6 +54271,7 @@ export {
53903
54271
  schemaPatrolRoute,
53904
54272
  schemaPerson,
53905
54273
  schemaPlate,
54274
+ schemaPost,
53906
54275
  schemaServiceProvider,
53907
54276
  schemaServiceProviderBilling,
53908
54277
  schemaSiteCamera,
@@ -54061,6 +54430,8 @@ export {
54061
54430
  usePatrolRouteRepo,
54062
54431
  usePersonController,
54063
54432
  usePersonRepo,
54433
+ usePostPrelovedController,
54434
+ usePostPrelovedRepo,
54064
54435
  usePriceController,
54065
54436
  usePriceModel,
54066
54437
  usePriceRepo,