Skip to content

Commit a195092

Browse files
committed
fix auth + add passwd
1 parent a314a96 commit a195092

17 files changed

+311
-17
lines changed

src/core/agents/_schemas/parts/security.part.schema.ts renamed to src/core/agents/_schemas/_parts/security.part.schema.ts

File renamed without changes.
File renamed without changes.

src/core/agents/_schemas/agents.schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
22
import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema';
3-
import { StatePart, StatePartSchema } from '~/core/agents/_schemas/parts/state.part.schema';
4-
import { SecurityPart, SecurityPartSchema } from '~/core/agents/_schemas/parts/security.part.schema';
3+
import { StatePart, StatePartSchema } from '~/core/agents/_schemas/_parts/state.part.schema';
4+
import { SecurityPart, SecurityPartSchema } from '~/core/agents/_schemas/_parts/security.part.schema';
55
import { MixedValue } from '~/_common/types/mixed-value.type';
66

77
const DEFAULT_THIRD_PARTY_AUTH = 'local';

src/core/agents/agents.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class AgentsController extends AbstractController {
2222
// entityId: 1,
2323
username: 1,
2424
displayName: 1,
25+
email: 1,
2526
state: 1,
2627
hidden: 1,
2728
};

src/core/agents/agents.service.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectModel } from '@nestjs/mongoose';
33
import { Agents } from '~/core/agents/_schemas/agents.schema';
4-
import { Model } from 'mongoose';
4+
import { Document, Model, ModifyResult, Query, QueryOptions, SaveOptions, Types, UpdateQuery } from 'mongoose';
55
import { AbstractServiceSchema } from '~/_common/abstracts/abstract.service.schema';
6+
import { AgentsCreateDto } from './_dto/agents.dto';
7+
import { hash } from 'argon2';
68

79
@Injectable()
810
export class AgentsService extends AbstractServiceSchema {
911
constructor(@InjectModel(Agents.name) protected _model: Model<Agents>) {
1012
super();
1113
}
14+
15+
public async create<T extends Agents | Document>(
16+
data?: AgentsCreateDto,
17+
options?: SaveOptions,
18+
): Promise<Document<T, any, T>> {
19+
data.password = await hash(data.password);
20+
return await super.create(data, options);
21+
}
22+
23+
public async update<T extends Agents | Document>(
24+
_id: Types.ObjectId | any,
25+
update: UpdateQuery<T> & any,
26+
options?: QueryOptions<T>,
27+
): Promise<ModifyResult<Query<T, T, any, T>>> {
28+
if (update.password) {
29+
update.password = await hash(update.password);
30+
}
31+
if (update.$set?.password) {
32+
update.$set.password = await hash(update.$set.password);
33+
}
34+
return await super.update(
35+
_id,
36+
{
37+
...update,
38+
$set: {
39+
...(update?.$set || {}),
40+
},
41+
},
42+
options,
43+
);
44+
}
1245
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
2+
import { Document } from 'mongoose';
3+
4+
export const DEFAULT_DATA_TYPE = 'default';
5+
6+
@Schema({ _id: false })
7+
export class DataPart extends Document {
8+
@Prop({
9+
type: String,
10+
default: DEFAULT_DATA_TYPE,
11+
})
12+
public type?: string;
13+
14+
@Prop({
15+
type: String,
16+
required: true,
17+
})
18+
public message: string;
19+
}
20+
21+
export const DataPartSchema = SchemaFactory.createForClass(DataPart);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
2+
import { Document } from 'mongoose';
3+
import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema';
4+
import { DataPart, DataPartSchema } from './_parts/data.part.schema';
5+
6+
export const DEFAULT_CONTEXT = 'default';
7+
export type LoggerDocument = Logger & Document;
8+
9+
@Schema({ versionKey: false })
10+
export class Logger extends AbstractSchema {
11+
@Prop({
12+
type: String,
13+
required: true,
14+
})
15+
public level: string;
16+
17+
@Prop({
18+
type: DataPartSchema,
19+
required: true,
20+
default: {},
21+
})
22+
public data: DataPart;
23+
24+
/**
25+
* @description The context of the log message.
26+
* @example 'identity'
27+
*/
28+
@Prop({ type: String })
29+
public context?: string;
30+
31+
/**
32+
* @description The concerned document in collection of the log message.
33+
* @example '5f7b1b3b7f7b1b3b7f7b1b3b'
34+
*/
35+
@Prop({ type: String })
36+
public concerned?: string;
37+
}
38+
39+
export const LoggerSchema = SchemaFactory.createForClass(Logger).pre(
40+
'save',
41+
function (this: Logger, next: () => void): void {
42+
this.context = this.context.toLocaleLowerCase() || DEFAULT_CONTEXT;
43+
this.concerned = this.concerned.toLocaleLowerCase() || null;
44+
next();
45+
},
46+
);

src/core/logger/internal.logger.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ export class InternalLogger extends ConsoleLogger {
144144
},
145145
options: InternalLogOptions,
146146
): void {
147-
console.log('logging');
148147
const data = payload.message instanceof Object ? payload.message : { message: payload.message };
149148
const metadata = {
150149
createdAt: new Date(),
@@ -161,8 +160,8 @@ export class InternalLogger extends ConsoleLogger {
161160
metadata,
162161
});
163162
} catch (e) {
164-
super.error('Failed to log to the database', e, this.constructor.name);
165-
console.log('Failed to log to the database', e);
163+
super.fatal('Failed to log to the database', e, this.constructor.name);
164+
super[payload.level](payload.message, ...payload.context);
166165
}
167166
}
168167
}

src/core/logger/logger.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Module } from '@nestjs/common';
22
import { MongooseModule } from '@nestjs/mongoose';
3-
import { LoggerSchema, Logger } from './schemas/logger.schema';
3+
import { LoggerSchema, Logger } from './_schemas/logger.schema';
44
import { LoggerService } from './logger.service';
55
import { LoggerController } from './logger.controller';
66

src/core/logger/schemas/logger.schema.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)