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']) {
|
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 user = await this.User.query().preload('contacts').preload('roles').where('id', id).first();
|
||||||
|
const { about } = data;
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
await user.merge(data).save();
|
await user.merge({ about }).save();
|
||||||
return { about: user.about };
|
return { about: user.about };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -248,9 +248,9 @@ export default class MeService {
|
|||||||
/**
|
/**
|
||||||
* Update user conctact
|
* Update user conctact
|
||||||
*
|
*
|
||||||
* @param data Data to update
|
* @param data Fields to update
|
||||||
* @param auth AuthContract
|
* @param auth AuthContract
|
||||||
* @returns Updated user contacts
|
* @returns IResponse
|
||||||
*/
|
*/
|
||||||
public async updateUserContacts(
|
public async updateUserContacts(
|
||||||
data: UpdateContactsValidator['schema']['props'],
|
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(
|
public async updateUserInfo(
|
||||||
data: UpdateUserInfoValidator['schema']['props'],
|
data: UpdateUserInfoValidator['schema']['props'],
|
||||||
auth: AuthContract
|
auth: AuthContract
|
||||||
): Promise<IResponse> {
|
): Promise<IResponse> {
|
||||||
const user = await auth.use(this.authGuard).authenticate();
|
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 {
|
return {
|
||||||
success: true,
|
success: false,
|
||||||
status: HttpStatusEnum.OK,
|
status: HttpStatusEnum.BAD_REQUEST,
|
||||||
message: 'User info updated.',
|
message: 'You must specify at least one field.',
|
||||||
data: {
|
data: {},
|
||||||
about,
|
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 {
|
return {
|
||||||
success: false,
|
success: true,
|
||||||
status: HttpStatusEnum.SERVICE_UNAVAILABLE,
|
status: HttpStatusEnum.OK,
|
||||||
message: 'Cannot update personal info.',
|
message: 'User contacts updated.',
|
||||||
data: {},
|
data: { ...data },
|
||||||
error: {
|
|
||||||
code: 'E_SERVICE_UNAVAILABLE',
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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';
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
||||||
|
|
||||||
export default class UpdateUserInfoValidator {
|
export default class UpdateUserInfoValidator {
|
||||||
constructor(protected ctx: HttpContextContract) {}
|
constructor(protected ctx: HttpContextContract) {}
|
||||||
|
|
||||||
public schema = schema.create({
|
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 = {};
|
public messages = {};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user