|
1 | 1 | import { FilterQuery } from 'mongoose';
|
2 | 2 | import { ApartmentType } from '../types';
|
| 3 | +import { UserIdSchemaType } from '../user/user.schema'; |
3 | 4 | import { JwtPayload } from '../utils/auth.utils';
|
4 | 5 | import { checkRecordForEmptyArrays } from '../utils/common.utils';
|
5 | 6 | import { getPaginator, GetPaginatorReturnType } from '../utils/getPaginator';
|
6 |
| -import { Apartment } from './apartment.model'; |
| 7 | +import { Apartment, IApartment } from './apartment.model'; |
7 | 8 | import {
|
8 | 9 | ApartmentCreateOrUpdateSchemaType,
|
9 | 10 | ApartmentIdSchemaType,
|
10 | 11 | ApartmentListQueryParamsType,
|
11 | 12 | } from './apartment.schema';
|
| 13 | +import { addNotificationJob } from '../queues/notification.queue'; |
| 14 | +import { |
| 15 | + NOTIFICATION_MESSAGES, |
| 16 | + NOTIFICATION_TITLE, |
| 17 | +} from '../notification/notification.constants'; |
12 | 18 |
|
13 | 19 | export interface IGetApartment {
|
14 | 20 | results: ApartmentType[];
|
@@ -108,14 +114,42 @@ export const getApartment = async (
|
108 | 114 |
|
109 | 115 | return result;
|
110 | 116 | };
|
| 117 | +export const getMyApartments = async ( |
| 118 | + userId: UserIdSchemaType, |
| 119 | +): Promise<IApartment[]> => { |
| 120 | + const result = await Apartment.find({ |
| 121 | + owner: userId.id, |
| 122 | + }).populate([ |
| 123 | + 'propertyType', |
| 124 | + 'typeOfPlace', |
| 125 | + 'cancellationPolicies', |
| 126 | + 'facilities', |
| 127 | + 'houseRules', |
| 128 | + 'discounts', |
| 129 | + 'bookingType', |
| 130 | + ]); |
| 131 | + |
| 132 | + if (!result) { |
| 133 | + throw new Error('Apartment not found'); |
| 134 | + } |
| 135 | + |
| 136 | + return result; |
| 137 | +}; |
111 | 138 |
|
112 | 139 | export const createApartment = async (
|
113 | 140 | body: ApartmentCreateOrUpdateSchemaType,
|
114 | 141 | user: JwtPayload,
|
115 | 142 | ): Promise<ApartmentType> => {
|
116 | 143 | const apartment = await Apartment.create({
|
117 | 144 | ...body,
|
118 |
| - userId: user.sub, |
| 145 | + owner: user.sub, |
| 146 | + }); |
| 147 | + |
| 148 | + await addNotificationJob({ |
| 149 | + title: NOTIFICATION_TITLE.NEW_LISTING, |
| 150 | + message: NOTIFICATION_MESSAGES.NEW_LISTING, |
| 151 | + notificationType: 'SYSTEM_NOTIFICATION', |
| 152 | + businessType: 'apartment', |
119 | 153 | });
|
120 | 154 |
|
121 | 155 | return apartment;
|
@@ -147,14 +181,18 @@ export const updateApartment = async (
|
147 | 181 |
|
148 | 182 | export const deleteApartment = async (
|
149 | 183 | apartmentId: ApartmentIdSchemaType,
|
| 184 | + userId: UserIdSchemaType, |
150 | 185 | ): Promise<void | Error> => {
|
151 | 186 | const { id } = apartmentId;
|
152 |
| - const deleted = await Apartment.deleteOne({ |
153 |
| - _id: id, |
154 |
| - }); |
155 | 187 |
|
156 |
| - if (deleted.deletedCount < 1) { |
| 188 | + const apartment = await Apartment.findById(id); |
| 189 | + |
| 190 | + if (!apartment) { |
157 | 191 | throw new Error('Apartment does not Exist');
|
| 192 | + } else if (apartment?.owner?.toString() !== userId.id.toString()) { |
| 193 | + throw new Error('You do not have permission to delete this apartment.'); |
| 194 | + } else { |
| 195 | + apartment.deleteOne(); |
158 | 196 | }
|
159 | 197 | };
|
160 | 198 |
|
|
0 commit comments