|
import { |
|
applyDecorators, |
|
Injectable, |
|
Param, |
|
PipeTransform, |
|
} from '@nestjs/common'; |
|
import { |
|
ApiExtraModels, |
|
ApiParam, |
|
ApiResponse, |
|
getSchemaPath, |
|
} from '@nestjs/swagger'; |
|
|
|
import { SessionManager } from '../core/abc/manager.abc'; |
|
import { WhatsappSession } from '../core/abc/session.abc'; |
|
import { Base64File } from '../structures/files.dto'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable() |
|
export class SessionPipe implements PipeTransform<WhatsappSession> { |
|
constructor(private manager: SessionManager) {} |
|
|
|
async transform(value: any) { |
|
return this.manager.getSession(value); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
export const SessionApiParam = ApiParam({ |
|
name: 'session', |
|
required: true, |
|
type: 'string', |
|
schema: { |
|
default: 'default', |
|
}, |
|
description: 'WhatsApp session name', |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
export const SessionParam = Param('session', SessionPipe); |
|
|
|
export const ChatIdApiParam = ApiParam({ |
|
name: 'chatId', |
|
required: true, |
|
type: 'string', |
|
description: 'Chat ID', |
|
example: '123456789@c.us', |
|
}); |
|
|
|
export const MessageIdApiParam = ApiParam({ |
|
name: 'messageId', |
|
required: true, |
|
type: 'string', |
|
description: 'Message ID', |
|
example: 'true_123456789@c.us_BAE6A33293978B16', |
|
}); |
|
|
|
function getRefSchemaPaths(models) { |
|
return models.map((model) => { |
|
return { $ref: getSchemaPath(model) }; |
|
}); |
|
} |
|
|
|
export function ApiFileAcceptHeader(...models) { |
|
models = models.length ? models : [Base64File]; |
|
return applyDecorators( |
|
|
|
|
|
ApiExtraModels(...models), |
|
ApiResponse({ |
|
status: 200, |
|
content: { |
|
'image/png': { |
|
schema: { |
|
type: 'string', |
|
format: 'binary', |
|
}, |
|
}, |
|
'application/json': { |
|
schema: { |
|
oneOf: getRefSchemaPaths(models), |
|
}, |
|
}, |
|
}, |
|
}), |
|
); |
|
} |
|
|