Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enmon contact email #448

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"git": {
"commitMessage": "chore: release v${version}",
"tagName": "v${version}",
"push": false
"push": true
},
"github": {
"release": true,
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
db:
uri: "mongodb://db/enmon-adapter"

enmon:
11 changes: 8 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ import { Config } from './config/schemas.js';
useFactory: (config: ConfigService<Config, true>) => ({
processEvery: '15 seconds',
db: {
address: config.get('db.uri', { infer: true }),
address: config.getOrThrow('db.uri', { infer: true }),
},
}),
}),
AlsModule.forRoot(),
MongooseModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<Config, true>) => ({
uri: config.get('db.uri', { infer: true }),
uri: config.getOrThrow('db.uri', { infer: true }),
}),
}),
ConfigModule.forRoot({
isGlobal: true,
load: [configuration],
cache: false,
}),
EnmonModule,
EnmonModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<Config, true>) => ({
contactEmail: config.getOrThrow('enmon.contactEmail', { infer: true }),
}),
}),
WinstonModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<Config, true>) => ({
Expand Down
27 changes: 27 additions & 0 deletions src/config/__snapshots__/parse.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ exports[`should reject invalid config 0 1`] = `
],
"received": "undefined",
},
{
"code": "invalid_type",
"expected": "object",
"message": "Required",
"path": [
"enmon",
],
"received": "undefined",
},
{
"code": "invalid_type",
"expected": "'UNI7xxx' | 'UNI1xxx'",
Expand Down Expand Up @@ -87,6 +96,15 @@ exports[`should reject invalid config 1 1`] = `
],
"received": "undefined",
},
{
"code": "invalid_type",
"expected": "object",
"message": "Required",
"path": [
"enmon",
],
"received": "undefined",
},
{
"code": "invalid_enum_value",
"message": "Invalid enum value. Expected 'UNI7xxx' | 'UNI1xxx', received 'invalid model'",
Expand Down Expand Up @@ -235,6 +253,15 @@ exports[`should reject invalid config 2 1`] = `
],
"validation": "url",
},
{
"code": "invalid_string",
"message": "Invalid email",
"path": [
"enmon",
"contactEmail",
],
"validation": "email",
},
{
"code": "invalid_enum_value",
"message": "Invalid enum value. Expected 'UNI7xxx' | 'UNI1xxx', received 'invalid model'",
Expand Down
7 changes: 7 additions & 0 deletions src/config/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const VALID_CONFIG_INPUT = {
db: {
uri: 'mongodb://host/dbname',
},
enmon: {
contactEmail: faker.internet.email(),
},
thermometers: [
{
model: ThermometerModel.UNI1xxx,
Expand Down Expand Up @@ -49,6 +52,7 @@ const VALID_CONFIG_INPUT = {
const VALID_CONFIG_OUTPUT = {
DEV: VALID_CONFIG_INPUT.DEV,
db: VALID_CONFIG_INPUT.db,
enmon: VALID_CONFIG_INPUT.enmon,
thermometers: VALID_CONFIG_INPUT.thermometers,
wattrouters: [
{
Expand Down Expand Up @@ -86,6 +90,9 @@ it.each([
db: {
uri: 'invalid URL',
},
enmon: {
contactEmail: 'invalid email',
},
thermometer: {
dataSourceUrl: 'invalid URL',
enmon: { customerId: 'invalid ID', devEUI: '', env: 'invalid EnmonEnv', token: '' },
Expand Down
3 changes: 3 additions & 0 deletions src/config/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const configSchema = z
db: z.object({
uri: z.string().url(),
}),
enmon: z.object({
contactEmail: z.string().email().optional(),
}),
thermometers: z.array(configThermometerSchema).nullish(),
wattrouter: configWattRouterSchema.nullish(),
wattrouters: z.array(configWattRouterSchema).nullish(),
Expand Down
7 changes: 5 additions & 2 deletions src/enmon/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface PostMeterPlainValueArgs {
}

export class EnmonApiClient {
constructor(public readonly env = EnmonEnv.App) {}
constructor(public readonly contactEmail?: string | undefined) {}

async postMeterPlainCounterMulti({
env,
Expand Down Expand Up @@ -70,7 +70,10 @@ export class EnmonApiClient {

private http({ env }: { env?: EnmonEnv | undefined }): AxiosInstance {
return axios.create({
baseURL: `https://${env ?? this.env}.enmon.tech`,
baseURL: `https://${env ?? EnmonEnv.App}.enmon.tech`,
headers: {
'User-Agent': `enmon-adapter (${this.contactEmail ?? '<email not provided>'})`,
},
});
}
}
73 changes: 51 additions & 22 deletions src/enmon/enmon.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { DynamicModule, FactoryProvider, Inject, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AgendaModule } from 'agenda-nest';
import { ReadingProcessor } from './reading.processor.js';
Expand All @@ -7,24 +7,53 @@ import { UploadReadingRepository } from './upload-reading.repository.js';
import { EnmonApiClient } from './ApiClient.js';
import { READINGS_QUEUE_NAME } from './constants.js';

@Module({
imports: [
AgendaModule.registerQueue(READINGS_QUEUE_NAME),
MongooseModule.forFeature([
{
name: UPLOAD_READING_MODEL_NAME,
schema: UploadReadingSchema,
},
]),
],
providers: [
ReadingProcessor,
UploadReadingRepository,
{
provide: EnmonApiClient,
useClass: EnmonApiClient,
},
],
exports: [UploadReadingRepository],
})
export class EnmonModule {}
export interface ModuleOptions {
contactEmail?: string | undefined;
}

const OPTIONS_TOKEN = Symbol('enmon/options');

export class EnmonModule implements OnApplicationBootstrap {
private readonly logger = new Logger(EnmonModule.name);

constructor(
@Inject(OPTIONS_TOKEN)
private readonly opts: ModuleOptions,
) {}

static forRootAsync(options: Pick<FactoryProvider<ModuleOptions>, 'inject' | 'useFactory'>): DynamicModule {
return {
module: EnmonModule,
imports: [
AgendaModule.registerQueue(READINGS_QUEUE_NAME),
MongooseModule.forFeature([
{
name: UPLOAD_READING_MODEL_NAME,
schema: UploadReadingSchema,
},
]),
],
providers: [
{
provide: OPTIONS_TOKEN,
inject: options.inject,
useFactory: options.useFactory,
} as FactoryProvider<ModuleOptions>,
ReadingProcessor,
UploadReadingRepository,
{
provide: EnmonApiClient,
inject: [OPTIONS_TOKEN],
useFactory: (opts: ModuleOptions) => new EnmonApiClient(opts.contactEmail),
},
],
exports: [UploadReadingRepository],
};
}

onApplicationBootstrap() {
if (!this.opts.contactEmail) {
this.logger.warn('Contact email not provided!');
}
}
}
3 changes: 3 additions & 0 deletions tests/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default (): Config =>
db: {
uri: 'mongodb://db/dbname',
},
enmon: {
contactEmail: 'test@test.tst',
},
thermometers: [
{
model: ThermometerModel.UNI7xxx,
Expand Down