Skip to content

Commit

Permalink
swapped req parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Oct 27, 2023
1 parent 2645188 commit 3f50d0f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 82 deletions.
16 changes: 13 additions & 3 deletions src/api/common/base.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request } from 'express';
import { compact } from 'lodash';
import { FilterQuery, Model } from 'mongoose';
import { UserProfile } from 'src/auth/dto/user-profile.dto';
import { CountDto } from './dto/count.dto';
export class BaseService<T> {
constructor(protected model: Model<T>) {}
Expand Down Expand Up @@ -34,7 +35,10 @@ export class BaseService<T> {
return res[0] ?? { count: 0 };
}

findAll(filter: any = {}): Promise<T[]> {
findAll(
filter: any = {},
req?: Request & { user: UserProfile },
): Promise<T[]> {
let { where, fields, order, skip, limit } = filter;
if (fields instanceof Array) {
fields = fields.reduce((p, c) => {
Expand Down Expand Up @@ -69,7 +73,10 @@ export class BaseService<T> {
.exec();
}

async findOne(filter: any = {}): Promise<T> {
async findOne(
filter: any = {},
req?: Request & { user: UserProfile },
): Promise<T> {
const res = await this.findAll({ ...filter, limit: 1 });
return res && res[0];
}
Expand Down Expand Up @@ -165,7 +172,10 @@ export class BaseService<T> {
this.model.findByIdAndRemove(id).exec();
}

async removeAll(filter?: FilterQuery<T>) {
async removeAll(
filter?: FilterQuery<T>,
req?: Request & { user: UserProfile },
) {
const castedFilter = this.model.countDocuments(filter).cast();
const res = await this.model
.aggregate(
Expand Down
72 changes: 45 additions & 27 deletions src/api/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ export class NotificationsController extends BaseController {
if (!currUser) {
throw new HttpException(undefined, HttpStatus.FORBIDDEN);
}
const instance = await this.notificationsService.findOne(this.req, {
where: { id },
});
const instance = await this.notificationsService.findOne(
{
where: { id },
},
this.req,
);
if (instance?.channel !== 'inApp') {
throw new HttpException(undefined, HttpStatus.FORBIDDEN);
}
Expand Down Expand Up @@ -187,20 +190,26 @@ export class NotificationsController extends BaseController {
@JsonQuery('filter')
filter: Omit<FilterDto<Notification>, 'where'>,
): Promise<Notification | null> {
return this.notificationsService.findOne(this.req, {
...filter,
where: { id },
});
return this.notificationsService.findOne(
{
...filter,
where: { id },
},
this.req,
);
}

@Delete(':id')
@ApiNoContentResponse({ description: 'Notification DELETE success' })
@ApiForbiddenResponse({ description: 'Forbidden' })
@HttpCode(204)
async deleteById(@Param('id') id: string): Promise<void> {
const data = await this.notificationsService.findOne(this.req, {
where: { id },
});
const data = await this.notificationsService.findOne(
{
where: { id },
},
this.req,
);
if (!data) throw new HttpException(undefined, HttpStatus.NOT_FOUND);
data.state = 'deleted';
await this.updateById(id, data);
Expand Down Expand Up @@ -230,7 +239,7 @@ export class NotificationsController extends BaseController {
@JsonQuery('filter')
filter: FilterDto<Notification>,
): Promise<Notification[]> {
const res = await this.notificationsService.findAll(this.req, filter);
const res = await this.notificationsService.findAll(filter, this.req);
if (res.length === 0) {
return res;
}
Expand Down Expand Up @@ -277,9 +286,12 @@ export class NotificationsController extends BaseController {
this.chunkRequestAborted = true;
});
}
const notification = await this.notificationsService.findOne(this.req, {
where: { id },
});
const notification = await this.notificationsService.findOne(
{
where: { id },
},
this.req,
);
if (!notification) throw new HttpException(undefined, HttpStatus.NOT_FOUND);
this.req['args'] = { data: notification };
this.req['NotifyBC.startIdx'] = startIdx;
Expand Down Expand Up @@ -445,12 +457,12 @@ export class NotificationsController extends BaseController {
data.dispatch?.skipped ?? [],
);
const subscribers = await this.subscriptionsService.findAll(
this.req,
{
where: {
id: { $in: subChunk },
},
},
this.req,
);
const notificationMsgCB = async (err: any, e: Subscription) => {
if (err) {
Expand Down Expand Up @@ -605,16 +617,19 @@ export class NotificationsController extends BaseController {
// main request
const postBroadcastProcessing = async () => {
data = await this.notificationsService.findById(data.id);
const res = await this.subscriptionsService.findAll(this.req, {
fields: {
userChannelId: true,
},
where: {
id: {
$in: data.dispatch?.successful,
const res = await this.subscriptionsService.findAll(
{
fields: {
userChannelId: true,
},
where: {
id: {
$in: data.dispatch?.successful,
},
},
},
});
this.req,
);
const userChannelIds = res.map((e) => e.userChannelId);
const errUserChannelIds = (data.dispatch?.failed || []).map(
(e: { userChannelId: any }) => e.userChannelId,
Expand Down Expand Up @@ -649,7 +664,6 @@ export class NotificationsController extends BaseController {
};

const subCandidates = await this.subscriptionsService.findAll(
this.req,
{
where: {
serviceName: data.serviceName,
Expand All @@ -658,6 +672,7 @@ export class NotificationsController extends BaseController {
},
fields: ['id'],
},
this.req,
);
data.dispatch = data.dispatch ?? {};
data.dispatch.candidates =
Expand Down Expand Up @@ -854,9 +869,12 @@ export class NotificationsController extends BaseController {
}

try {
const subscription = await this.subscriptionsService.findOne(this.req, {
where: whereClause,
});
const subscription = await this.subscriptionsService.findOne(
{
where: whereClause,
},
this.req,
);
if (!subscription) {
throw new HttpException('invalid user', HttpStatus.FORBIDDEN);
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ export class NotificationsService extends BaseService<Notification> {
}

findAll(
req: Request & { user: UserProfile },
filter: FilterDto<Notification> = {},
req: Request & { user: UserProfile },
) {
filter.where = this.accessInterceptor(req, filter.where);
return super.findAll(filter);
}

async findOne(
req: Request & { user: UserProfile },
filter: FilterDto<Notification> = {},
req: Request & { user: UserProfile },
): Promise<Notification> {
filter.where = this.accessInterceptor(req, filter.where);
return super.findOne(filter);
Expand All @@ -70,8 +70,8 @@ export class NotificationsService extends BaseService<Notification> {
}

async removeAll(
req: Request & { user: UserProfile },
where?: FilterQuery<Notification>,
where: FilterQuery<Notification>,
req?: Request & { user: UserProfile },
) {
where = this.accessInterceptor(req, where);
return super.removeAll(where);
Expand Down
Loading

0 comments on commit 3f50d0f

Please sign in to comment.