Skip to content

Commit

Permalink
ported SubscriptionBeforeSaveInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Oct 15, 2023
1 parent ad8b667 commit 108c2c2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// file ported
import {
injectable,
Interceptor,
Expand Down Expand Up @@ -70,6 +71,7 @@ export class SubscriptionBeforeSaveInterceptor
case 'replaceById':
argIdx = 1;
}

const data = invocationCtx.args[argIdx];
if (!data) {
return next();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"class-validator": "^0.14.0",
"crypto-random-string": "^3.3.0",
"ip-range-check": "^0.2.0",
"jmespath": "f-w/jmespath.js#semver:^1.0",
"lodash": "^4.17.21",
"mongodb-memory-server": "^8.15.1",
"mongoose": "^7.4.5",
Expand Down
34 changes: 34 additions & 0 deletions src/api/subscriptions/broadcast-push-notification-filter.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ArgumentMetadata,
HttpException,
HttpStatus,
Injectable,
PipeTransform,
} from '@nestjs/common';
import jmespath from 'jmespath';

@Injectable()
export class BroadcastPushNotificationFilterPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
let filter = value.broadcastPushNotificationFilter;
if (!filter) {
return value;
}
if (typeof filter !== 'string') {
throw new HttpException(
'invalid broadcastPushNotificationFilter',
HttpStatus.BAD_REQUEST,
);
}
filter = '[?' + filter + ']';
try {
jmespath.compile(filter);
} catch (ex) {
throw new HttpException(
'invalid broadcastPushNotificationFilter',
HttpStatus.BAD_REQUEST,
);
}
return value;
}
}
10 changes: 7 additions & 3 deletions src/api/subscriptions/subscriptions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
JsonQuery,
} from '../common/json-query.decorator';
import { ConfigurationsService } from '../configurations/configurations.service';
import { BroadcastPushNotificationFilterPipe } from './broadcast-push-notification-filter.pipe';
import { CreateSubscriptionDto } from './dto/create-subscription.dto';
import { UpdateSubscriptionDto } from './dto/update-subscription.dto';
import { Subscription } from './entities/subscription.entity';
Expand Down Expand Up @@ -430,7 +431,8 @@ export class SubscriptionsController extends BaseController {
async replaceById(
@Req() req: Request & { user: UserProfile },
@Param('id') id: string,
@Body() subscription: CreateSubscriptionDto,
@Body(BroadcastPushNotificationFilterPipe)
subscription: CreateSubscriptionDto,
): Promise<Subscription> {
return this.subscriptionsService.replaceById(id, subscription, req);
}
Expand All @@ -445,7 +447,8 @@ export class SubscriptionsController extends BaseController {
async updateById(
@Req() req: Request & { user: UserProfile },
@Param('id') id: string,
@Body() subscription: UpdateSubscriptionDto,
@Body(BroadcastPushNotificationFilterPipe)
subscription: UpdateSubscriptionDto,
): Promise<Subscription> {
const instance = await this.subscriptionsService.findOne(req, {
where: { id },
Expand Down Expand Up @@ -730,7 +733,8 @@ export class SubscriptionsController extends BaseController {
@HttpCode(200)
async create(
@Req() req: Request & { user: UserProfile },
@Body() subscription: CreateSubscriptionDto,
@Body(BroadcastPushNotificationFilterPipe)
subscription: CreateSubscriptionDto,
): Promise<Subscription> {
if (![Role.SuperAdmin, Role.Admin].includes(req.user?.role)) {
delete subscription.state;
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4225,6 +4225,10 @@ jest@^29.5.0:
import-local "^3.0.2"
jest-cli "^29.6.2"

"jmespath@f-w/jmespath.js#semver:^1.0":
version "1.0.0"
resolved "https://codeload.github.com/f-w/jmespath.js/tar.gz/8782ba3794272d278704c9945126fd6f9b61022d"

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 108c2c2

Please sign in to comment.