-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): added push API module to save PushSubscriptions
- Loading branch information
Showing
21 changed files
with
217 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ApiModelProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString, IsUrl } from 'class-validator'; | ||
|
||
export class CreateSubscriptionDto { | ||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsUrl({}, { message: 'endpoint must be a valid url.' }) | ||
endpoint: string; | ||
|
||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsString() | ||
auth: string; | ||
|
||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsString() | ||
p256dh: string; | ||
|
||
@ApiModelProperty({ type: String, isArray: true }) | ||
@IsNotEmpty() | ||
topics: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './push.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PushController } from './push.controller'; | ||
|
||
describe('Push Controller', () => { | ||
let module: TestingModule; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
controllers: [PushController], | ||
}).compile(); | ||
}); | ||
it('should be defined', () => { | ||
const controller: PushController = module.get<PushController>(PushController); | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Body, Controller, HttpStatus, Post } from '@nestjs/common'; | ||
import { ApiOAuth2Auth, ApiOperation, ApiResponse, ApiUseTags } from '@nestjs/swagger'; | ||
import { CrudController } from '../core'; | ||
import { Subscription } from './subscription.entity'; | ||
import { PushService } from './push.service'; | ||
import { CreateSubscriptionDto } from './dto/create-subscription.dto'; | ||
import { CurrentUser, User } from '../auth'; | ||
|
||
@ApiOAuth2Auth(['read']) | ||
@ApiUseTags('Sumo', 'Push') | ||
@Controller() | ||
export class PushController extends CrudController<Subscription> { | ||
constructor(private readonly pushService: PushService) { | ||
super(pushService); | ||
} | ||
|
||
@ApiOperation({ title: 'Create new record' }) | ||
@ApiResponse({ | ||
status: HttpStatus.CREATED, | ||
description: 'The record has been successfully created.', | ||
type: Subscription, | ||
}) | ||
@ApiResponse({ | ||
status: HttpStatus.BAD_REQUEST, | ||
description: 'Invalid input, The response body may contain clues as to what went wrong', | ||
}) | ||
@Post() | ||
async create(@Body() entity: CreateSubscriptionDto, @CurrentUser() user: User): Promise<Subscription> { | ||
return super.create({ ...entity, userId: user.userId }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { PushController } from './push.controller'; | ||
import { PushService } from './push.service'; | ||
import { Subscription } from './subscription.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Subscription])], | ||
providers: [PushService], | ||
controllers: [PushController], | ||
}) | ||
export class PushModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PushService } from './push.service'; | ||
|
||
describe('PushService', () => { | ||
let service: PushService; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [PushService], | ||
}).compile(); | ||
service = module.get<PushService>(PushService); | ||
}); | ||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { CrudService } from '../core'; | ||
import { Repository } from 'typeorm'; | ||
import { Subscription } from './subscription.entity'; | ||
|
||
@Injectable() | ||
export class PushService extends CrudService<Subscription> { | ||
constructor(@InjectRepository(Subscription) private readonly subscriptionRepository: Repository<Subscription>) { | ||
super(subscriptionRepository); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Column, CreateDateColumn, Entity, Index, UpdateDateColumn, VersionColumn } from 'typeorm'; | ||
import { ApiModelProperty } from '@nestjs/swagger'; | ||
import { IsAscii, IsNotEmpty, IsString, IsUrl, MaxLength, MinLength } from 'class-validator'; | ||
import { Exclude } from 'class-transformer'; | ||
import { Base } from '../core/entities/base.entity'; | ||
|
||
@Entity('subscription') | ||
export class Subscription extends Base { | ||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsUrl({}, { message: 'endpoint must be a valid url.' }) | ||
@Index({ unique: true }) | ||
@Column() | ||
endpoint: string; | ||
|
||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsString() | ||
@Column({}) | ||
auth: string; | ||
|
||
@ApiModelProperty({ type: String }) | ||
@IsNotEmpty() | ||
@IsString() | ||
@Column({}) | ||
p256dh: string; | ||
|
||
@ApiModelProperty({ type: String, minLength: 8, maxLength: 20 }) | ||
@IsAscii() | ||
@IsNotEmpty() | ||
@MinLength(8) | ||
@MaxLength(20) | ||
@Index() | ||
@Column() | ||
userId: string; | ||
|
||
@ApiModelProperty({ type: String, isArray: true }) | ||
@Column('text', { array: true }) | ||
topics: string[]; | ||
|
||
@ApiModelProperty({ type: Date }) | ||
@CreateDateColumn() | ||
createdAt?: Date; | ||
|
||
@ApiModelProperty({ type: Date }) | ||
@UpdateDateColumn() | ||
updatedAt?: Date; | ||
|
||
@Exclude() | ||
@VersionColumn() | ||
version?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.