mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 18:29:15 +03:00
updateContacts method got MeController
This commit is contained in:
parent
6d32ca0857
commit
df1d1716d5
@ -4,6 +4,7 @@ import Hash from '@ioc:Adonis/Core/Hash';
|
|||||||
import Logger from '@ioc:Adonis/Core/Logger';
|
import Logger from '@ioc:Adonis/Core/Logger';
|
||||||
import ChangePasswordValidator from 'App/Validators/ChangePasswordValidator';
|
import ChangePasswordValidator from 'App/Validators/ChangePasswordValidator';
|
||||||
import Contact from 'App/Models/Contact';
|
import Contact from 'App/Models/Contact';
|
||||||
|
import UpdateContactsValidator from 'App/Validators/UpdateContactsValidator';
|
||||||
|
|
||||||
export default class MeController {
|
export default class MeController {
|
||||||
private readonly guard: 'api';
|
private readonly guard: 'api';
|
||||||
@ -69,5 +70,25 @@ export default class MeController {
|
|||||||
* Update contacts of authenticated user
|
* Update contacts of authenticated user
|
||||||
* PATCH /me/contacts
|
* PATCH /me/contacts
|
||||||
*/
|
*/
|
||||||
// public async updateContacts({ request, response, auth }: HttpContextContract) {}
|
public async updateContacts({ request, response, auth }: HttpContextContract) {
|
||||||
|
const payload = await request.validate(UpdateContactsValidator);
|
||||||
|
const user = await auth.use(this.guard).authenticate();
|
||||||
|
const contacts = await this.contact.findByOrFail('user_id', user.id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update contacts
|
||||||
|
*/
|
||||||
|
if (payload.email) contacts.email = payload.email;
|
||||||
|
contacts.phone_number = payload.phone_number ?? null;
|
||||||
|
contacts.vk_id = payload.vk_id ?? null;
|
||||||
|
contacts.twitter_id = payload.twitter_id ?? null;
|
||||||
|
contacts.telegram_id = payload.telegram_id ?? null;
|
||||||
|
|
||||||
|
await contacts.save();
|
||||||
|
|
||||||
|
return response.ok({
|
||||||
|
message: `Contacts has been successfully updated.`,
|
||||||
|
data: contacts,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,7 +78,7 @@ export default class UsersController {
|
|||||||
await request.validate(UpdateUserValidator);
|
await request.validate(UpdateUserValidator);
|
||||||
|
|
||||||
const user = await this.user.findOrFail(params.id);
|
const user = await this.user.findOrFail(params.id);
|
||||||
const dataInput: Pick<User, 'first_name' | 'last_name' | 'login' | 'password'> = {
|
const input: Pick<User, 'first_name' | 'last_name' | 'login' | 'password'> = {
|
||||||
first_name: request.input('first_name'),
|
first_name: request.input('first_name'),
|
||||||
last_name: request.input('last_name'),
|
last_name: request.input('last_name'),
|
||||||
login: request.input('login'),
|
login: request.input('login'),
|
||||||
@ -86,11 +86,11 @@ export default class UsersController {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update only provided input roles value.
|
* Update roles
|
||||||
*/
|
*/
|
||||||
Object.keys(dataInput).forEach(k => {
|
Object.keys(input).forEach(k => {
|
||||||
if (dataInput[k] !== undefined) {
|
if (input[k] !== undefined) {
|
||||||
user[k] = dataInput[k];
|
user[k] = input[k];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -12,16 +12,16 @@ export default class Contact extends BaseModel {
|
|||||||
public email: string;
|
public email: string;
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public phone_number: string;
|
public phone_number: string | null;
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public vk_id: string;
|
public vk_id: string | null;
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public twitter_id: string;
|
public twitter_id: string | null;
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public telegram_id: string;
|
public telegram_id: string | null;
|
||||||
|
|
||||||
@column.dateTime({ autoCreate: true, serializeAs: null })
|
@column.dateTime({ autoCreate: true, serializeAs: null })
|
||||||
public createdAt: DateTime;
|
public createdAt: DateTime;
|
||||||
|
|||||||
@ -5,11 +5,16 @@ export default class UpdateContactsValidator {
|
|||||||
constructor(protected ctx: HttpContextContract) {}
|
constructor(protected ctx: HttpContextContract) {}
|
||||||
|
|
||||||
public schema = schema.create({
|
public schema = schema.create({
|
||||||
email: schema.string.optional({}, [rules.email(), rules.unique({ table: 'contacts', column: 'email' })]),
|
email: schema.string.optional({}, [rules.email()]),
|
||||||
phone_number: schema.string.optional(),
|
phone_number: schema.string.optional({}, [
|
||||||
vk_id: schema.string.optional(),
|
rules.mobile({
|
||||||
twitter_id: schema.string.optional(),
|
locales: ['ru-RU', 'en-US'],
|
||||||
telegram_id: schema.string.optional(),
|
strict: true,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
vk_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]),
|
||||||
|
twitter_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]),
|
||||||
|
telegram_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]),
|
||||||
});
|
});
|
||||||
|
|
||||||
public messages = {};
|
public messages = {};
|
||||||
|
|||||||
@ -47,7 +47,7 @@ Route.group(() => {
|
|||||||
Route.patch('me/password', 'MeController.changePassword')
|
Route.patch('me/password', 'MeController.changePassword')
|
||||||
.middleware('role:admin,teacher,student')
|
.middleware('role:admin,teacher,student')
|
||||||
.as('changePassword');
|
.as('changePassword');
|
||||||
Route.patch('me/contacts', 'MeController.updateContacts')
|
Route.put('me/contacts', 'MeController.updateContacts')
|
||||||
.middleware('role:admin,teacher,student')
|
.middleware('role:admin,teacher,student')
|
||||||
.as('updateContacts');
|
.as('updateContacts');
|
||||||
}).middleware('auth');
|
}).middleware('auth');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user