-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromoBanner.model.js
180 lines (155 loc) · 4.56 KB
/
promoBanner.model.js
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
const mongoose = require("mongoose");
const PromoBanner = require("../db/model/PromoBanner.js");
module.exports.createPromoBanner = async (body) => {
return new Promise(async (resolve) => {
try {
const { key, name, description, banners } = body;
const bannerExists = await this.checkIfPromoBannerExistsWIthKey(key);
if (bannerExists) {
return resolve({ already: "Banner Already exists with key" });
}
const banner = await PromoBanner.create({
key,
name,
description,
banners,
});
if (!banner || !"key" in banner) {
return resolve({
error: "error while creating banner",
});
}
return resolve(banner.toObject());
} catch (error) {
resolve({ error });
}
});
};
module.exports.getAllPromoBanners = async (search, page, limit) => {
try {
const searchQuery = search || "";
const mongoLimit = limit || 8;
const mongoSkip = page ? (parseInt(page) - 1) * mongoLimit : 0;
const query = [];
if (searchQuery.trim().length > 1) {
query.push({ $match: { $text: { $search: searchQuery } } });
}
const projection = {
createdOn: 0,
};
// Add Pagination
query.push(
{ $sort: { _id: -1 } },
{ $skip: mongoSkip },
{ $limit: mongoLimit },
{ $project: projection }
);
return await PromoBanner.aggregate(query);
} catch (error) {
return { error };
}
};
module.exports.getPromoBannerById = async (id) => {
return new Promise(async (resolve) => {
try {
const banner = await PromoBanner.findOne({
_id: new mongoose.Types.ObjectId(id),
});
if (!banner || !"key" in banner) {
return resolve({ notFound: `No Banner found` });
}
return resolve(banner.toObject());
} catch (error) {
resolve({ error });
}
});
};
module.exports.getPromoBannerByKey = async (key) => {
return new Promise(async (resolve) => {
try {
const banner = await PromoBanner.findOne({
key,
});
if (!banner || !"key" in banner) {
return resolve({ notFound: `No Banner found` });
}
return resolve(banner.toObject());
} catch (error) {
resolve({ error });
}
});
};
module.exports.deleteAllPromoBanners = async () => {
return new Promise(async (resolve) => {
try {
const banner = await PromoBanner.deleteMany();
if (!banner) {
return resolve({ notFound: `No Banner found` });
}
return resolve({ data: "All PromoBanners deleted successfully" });
} catch (error) {
resolve({ error });
}
});
};
module.exports.updatePromoBanner = async (id, body, projection) => {
return new Promise(async (resolve) => {
try {
const updateExpression = {};
// Validate the incoming data
const fieldsToUpdate = ["key", "name", "description", "banners"];
for (const field of fieldsToUpdate) {
if (fieldsToUpdate.includes(field)) {
updateExpression[field] = body[field];
}
}
// Check is Banner already exists
const bannerExists = await this.checkIfPromoBannerExistsWIthKey(body.key);
if (bannerExists) {
return resolve({ already: "Banner Already exists with key" });
}
// Update Document
const updatedResult = await PromoBanner.findOneAndUpdate(
{ _id: new mongoose.Types.ObjectId(id) },
updateExpression,
{
new: true,
projection: projection || {},
}
);
if (!updatedResult || !"name" in updatedResult) {
return resolve({ notFound: `No Banner found` });
}
return resolve(updatedResult.toObject());
} catch (error) {
return resolve({ error });
}
});
};
module.exports.deletePromoBanner = async (id) => {
return new Promise(async (resolve) => {
try {
const banner = await PromoBanner.deleteOne({
_id: new mongoose.Types.ObjectId(id),
});
if (!banner || !banner.acknowledged || banner.deletedCount === 0) {
return resolve({
notFound: `No Banner found`,
});
}
return resolve({ data: "Banner Deleted successfully" });
} catch (error) {
resolve({ error });
}
});
};
module.exports.checkIfPromoBannerExistsWIthKey = async (key) => {
const banner = await PromoBanner.findOne({ key });
return banner != null ? true : false;
};
module.exports.checkIfPromoBannerExistsById = async (id) => {
const banner = await PromoBanner.findOne({
_id: new mongoose.Types.ObjectId(id),
});
return banner != null ? true : false;
};