mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
feat: updating info of authorized user
This commit is contained in:
parent
9dc74376f2
commit
452d7d9557
@ -177,9 +177,10 @@ export default class UserRepository {
|
||||
*/
|
||||
public async updateInfo(id: string | number, data: UpdateUserInfoValidator['schema']['props']) {
|
||||
const user = await this.User.query().preload('contacts').preload('roles').where('id', id).first();
|
||||
const { about } = data;
|
||||
|
||||
if (user) {
|
||||
await user.merge(data).save();
|
||||
await user.merge({ about }).save();
|
||||
return { about: user.about };
|
||||
}
|
||||
|
||||
|
||||
@ -248,9 +248,9 @@ export default class MeService {
|
||||
/**
|
||||
* Update user conctact
|
||||
*
|
||||
* @param data Data to update
|
||||
* @param data Fields to update
|
||||
* @param auth AuthContract
|
||||
* @returns Updated user contacts
|
||||
* @returns IResponse
|
||||
*/
|
||||
public async updateUserContacts(
|
||||
data: UpdateContactsValidator['schema']['props'],
|
||||
@ -281,34 +281,59 @@ export default class MeService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update info about user
|
||||
*
|
||||
* @param data Fields to update
|
||||
* @param auth AuthContract
|
||||
* @returns IResponse
|
||||
*/
|
||||
public async updateUserInfo(
|
||||
data: UpdateUserInfoValidator['schema']['props'],
|
||||
auth: AuthContract
|
||||
): Promise<IResponse> {
|
||||
const user = await auth.use(this.authGuard).authenticate();
|
||||
const info = await this.userRepository.updateInfo(user.id, data);
|
||||
|
||||
if (info) {
|
||||
const { about } = info;
|
||||
|
||||
if (Object.keys(data).length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
status: HttpStatusEnum.OK,
|
||||
message: 'User info updated.',
|
||||
data: {
|
||||
about,
|
||||
success: false,
|
||||
status: HttpStatusEnum.BAD_REQUEST,
|
||||
message: 'You must specify at least one field.',
|
||||
data: {},
|
||||
error: {
|
||||
code: 'E_BAD_REQUEST',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Personal info updating
|
||||
*/
|
||||
if (data.about !== undefined) {
|
||||
await this.userRepository.updateInfo(user.id, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contacts updating
|
||||
*/
|
||||
if (data.phone_number !== undefined || data.twitter_id !== undefined || data.telegram_id !== undefined) {
|
||||
const contacts = await this.contactRepository.update(user, {
|
||||
phone_number: data.phone_number,
|
||||
twitter_id: data.twitter_id,
|
||||
telegram_id: data.telegram_id,
|
||||
vk_id: data.vk_id,
|
||||
});
|
||||
|
||||
if (!contacts) {
|
||||
await this.contactRepository.create(user, data);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
status: HttpStatusEnum.SERVICE_UNAVAILABLE,
|
||||
message: 'Cannot update personal info.',
|
||||
data: {},
|
||||
error: {
|
||||
code: 'E_SERVICE_UNAVAILABLE',
|
||||
},
|
||||
success: true,
|
||||
status: HttpStatusEnum.OK,
|
||||
message: 'User contacts updated.',
|
||||
data: { ...data },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,18 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator';
|
||||
import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
||||
|
||||
export default class UpdateUserInfoValidator {
|
||||
constructor(protected ctx: HttpContextContract) {}
|
||||
|
||||
public schema = schema.create({
|
||||
about: schema.string.optional(),
|
||||
about: schema.string.nullableAndOptional(),
|
||||
phone_number: schema.string.nullableAndOptional({}, [
|
||||
rules.mobile({ locales: ['ru-RU', 'en-US'], strict: true }),
|
||||
rules.unique({ table: 'contacts', column: 'phone_number' }),
|
||||
]),
|
||||
vk_id: schema.string.nullableAndOptional({}, [rules.regex(/^([a-zA-Z0-9_]){1,64}$/)]),
|
||||
twitter_id: schema.string.nullableAndOptional({}, [rules.regex(/(^|[^@\w])@(\w{1,15})\b/)]),
|
||||
telegram_id: schema.string.nullableAndOptional({}, [rules.regex(/(^|[^@\w])@(\w{1,64})\b/)]),
|
||||
});
|
||||
|
||||
public messages = {};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user