Skip to content

Commit 28e3dda

Browse files
authored
Merge pull request #21 from Libertech-FR/14-api-feature
WIP implements mails
2 parents b78ffe1 + 3ca476d commit 28e3dda

37 files changed

+950
-68
lines changed

app/src/public/favicon.ico

0 Bytes
Binary file not shown.

service/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
"cookie-parser": "^1.4.6",
3838
"dayjs": "^1.11.9",
3939
"deepmerge": "^4.3.1",
40+
"hbs": "^4.2.0",
4041
"helmet": "^7.0.0",
4142
"ioredis": "^5.3.2",
43+
"mailparser": "^3.6.5",
4244
"mongoose": "^7.5.0",
4345
"mongoose-unique-validator": "^4.0.0",
4446
"nestjs-i18n": "^10.3.1",
@@ -59,6 +61,7 @@
5961
"@types/cookie-parser": "^1.4.3",
6062
"@types/express": "^4.17.13",
6163
"@types/jest": "29.5.0",
64+
"@types/mailparser": "^3.4.0",
6265
"@types/multer": "^1.4.7",
6366
"@types/node": "18.16.0",
6467
"@types/passport": "^1.0.12",

service/public/favicon.ico

4.19 KB
Binary file not shown.

service/src/_common/abstracts/abstract.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ModuleRef } from '@nestjs/core'
22
import { ApiExtraModels } from '@nestjs/swagger'
33
import { PaginatedResponseDto } from '~/_common/dto/paginated-response.dto'
4+
import { Logger } from '@nestjs/common'
45

56
export interface AbstractControllerContext {
67
[key: string | number]: any
@@ -10,10 +11,12 @@ export interface AbstractControllerContext {
1011

1112
@ApiExtraModels(PaginatedResponseDto)
1213
export abstract class AbstractController {
14+
protected logger: Logger
1315
protected moduleRef?: ModuleRef
1416

1517
protected constructor(context?: AbstractControllerContext) {
1618
this.moduleRef = context?.moduleRef
19+
this.logger = new Logger(this.controllerName)
1720
}
1821

1922
public get controllerName(): string {

service/src/_common/abstracts/abstract.service.schema.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'
22
import { AbstractSchema } from './schemas/abstract.schema'
33
import {
44
Document,
5-
FilterQuery,
5+
FilterQuery, HydratedDocument,
66
Model,
77
ModifyResult,
88
ProjectionType,
@@ -15,6 +15,7 @@ import {
1515
import { AbstractService, AbstractServiceContext } from './abstract.service'
1616
import { ServiceSchemaInterface } from './interfaces/service.schema.interface'
1717
import { EventEmitterSeparator } from '~/_common/constants/event-emitter.constant'
18+
import { Filestorage } from '~/core/filestorage/_schemas/filestorage.schema'
1819

1920
@Injectable()
2021
export abstract class AbstractServiceSchema extends AbstractService implements ServiceSchemaInterface {
@@ -161,7 +162,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
161162
session: typeof arguments[1] === 'object' && 'session' in arguments[1] ? 'session' : undefined,
162163
},
163164
})
164-
this.logger.debug(['create', JSON.stringify(Object.values(arguments))].join(' '))
165+
this.logger.debug(['create', JSON.stringify(logInfos)].join(' '))
165166
if (this.eventEmitter) {
166167
const beforeEvents = await this.eventEmitter?.emitAsync([
167168
this.moduleName.toLowerCase(),
@@ -177,13 +178,13 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
177178
}
178179
console.log('this.request?.user', this.request?.user)
179180
const document: Document<T, any, T> = new this._model({
180-
...data,
181181
metadata: {
182182
createdBy: this.request?.user?.username || 'anonymous',
183183
createdAt: new Date(),
184184
lastUpdatedBy: this.request?.user?.username || 'anonymous',
185185
lastUpdatedAt: new Date(),
186186
},
187+
...data,
187188
})
188189
let created = document.save(options)
189190
if (this.eventEmitter) {

service/src/_common/abstracts/dto/custom-fields.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export class CustomFieldsDto {
44
@IsObject()
55
@IsOptional()
66
@ValidateNested()
7-
public customFields?: { [key: string]: any }[]
7+
public customFields?: { [key: string]: any }
88
}

service/src/_common/abstracts/interfaces/service.schema.interface.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
import { Document, FilterQuery, ModifyResult, ProjectionType, Query, QueryOptions, SaveOptions, Types, UpdateQuery } from 'mongoose'
1+
import {
2+
Document,
3+
FilterQuery,
4+
HydratedDocument,
5+
ModifyResult,
6+
ProjectionType,
7+
Query,
8+
QueryOptions,
9+
SaveOptions,
10+
Types,
11+
UpdateQuery,
12+
} from 'mongoose'
213
import { AbstractSchema } from '../schemas/abstract.schema'
14+
import { Filestorage } from '~/core/filestorage/_schemas/filestorage.schema'
315

416
export interface ServiceSchemaInterface {
517
find<T extends AbstractSchema | Document>(

service/src/_common/dto/parts/idname.part.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { IsMongoId, IsNotEmpty, IsString } from 'class-validator'
22
import { ApiProperty } from '@nestjs/swagger'
3+
import { Types } from 'mongoose'
34

45
export class IdnamePartDto {
56
@IsMongoId({ message: 'Id invalide' })
67
@ApiProperty()
7-
public id: string
8+
public id: Types.ObjectId
89

910
@IsString()
1011
@IsNotEmpty()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { IsMongoId, IsNotEmpty, IsOptional, IsString } from 'class-validator'
2+
import { ApiProperty } from '@nestjs/swagger'
3+
import { Types } from 'mongoose'
4+
5+
export class MailaddressPartDto {
6+
@IsString()
7+
@IsNotEmpty()
8+
@ApiProperty()
9+
public address: string
10+
11+
@IsString()
12+
@IsOptional()
13+
@ApiProperty()
14+
public name: string
15+
16+
@IsMongoId({ message: 'Id invalide' })
17+
@IsOptional()
18+
@ApiProperty()
19+
public entityId?: Types.ObjectId
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { CallHandler, ExecutionContext, Inject, mixin, NestInterceptor, Optional, Type } from '@nestjs/common'
2+
import { Observable } from 'rxjs'
3+
import * as multer from 'multer'
4+
import { MULTER_MODULE_OPTIONS } from '@nestjs/platform-express/multer/files.constants'
5+
import { MulterModuleOptions } from '@nestjs/platform-express'
6+
import { transformException } from '@nestjs/platform-express/multer/multer/multer.utils'
7+
import { Request } from 'express'
8+
9+
export function FileRawBodyInterceptor(fieldName: string, localOptions?: any): Type<NestInterceptor> {
10+
class MixinInterceptor implements NestInterceptor {
11+
protected multer: any
12+
13+
public constructor(
14+
@Optional()
15+
@Inject(MULTER_MODULE_OPTIONS) options: MulterModuleOptions = {},
16+
) {
17+
this.multer = (multer as any)({
18+
...options,
19+
...localOptions,
20+
})
21+
}
22+
23+
public async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
24+
const ctx = context.switchToHttp()
25+
await new Promise<void>((resolve, reject) => {
26+
const request = ctx.getRequest<Request & { rawBody: string }>()
27+
request.rawBody = ''
28+
request.prependListener('data', (chunk) => request.rawBody += chunk.toString())
29+
this.multer.single(fieldName)(
30+
request,
31+
ctx.getResponse(),
32+
(err: any) => {
33+
if (err) {
34+
const error = transformException(err)
35+
return reject(error)
36+
}
37+
resolve()
38+
},
39+
)
40+
},
41+
)
42+
return next.handle()
43+
}
44+
}
45+
return mixin(MixinInterceptor)
46+
}

0 commit comments

Comments
 (0)