|
| 1 | +import { Request } from 'express' |
| 2 | +import models from 'models' |
| 3 | +import ResponseError from 'modules/Response/ResponseError' |
| 4 | +import useValidation from 'helpers/useValidation' |
| 5 | +import PluginSqlizeQuery from 'modules/SqlizeQuery/PluginSqlizeQuery' |
| 6 | +import schema from 'controllers/Session/schema' |
| 7 | +import { SessionAttributes } from 'models/session' |
| 8 | + |
| 9 | +const { Session } = models |
| 10 | + |
| 11 | +class SessionService { |
| 12 | + /** |
| 13 | + * |
| 14 | + * @param req Request |
| 15 | + */ |
| 16 | + public static async getAll(req: Request) { |
| 17 | + const { includeCount, order, ...queryFind } = PluginSqlizeQuery.generate( |
| 18 | + req.query, |
| 19 | + Session, |
| 20 | + [] |
| 21 | + ) |
| 22 | + |
| 23 | + const data = await Session.findAll({ |
| 24 | + ...queryFind, |
| 25 | + order: order.length ? order : [['createdAt', 'desc']], |
| 26 | + }) |
| 27 | + const total = await Session.count({ |
| 28 | + include: includeCount, |
| 29 | + where: queryFind.where, |
| 30 | + }) |
| 31 | + |
| 32 | + return { message: `${total} data has been received.`, data, total } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + * @param id |
| 38 | + */ |
| 39 | + public static async getOne(id: string) { |
| 40 | + const data = await Session.findByPk(id) |
| 41 | + |
| 42 | + if (!data) { |
| 43 | + throw new ResponseError.NotFound( |
| 44 | + 'session data not found or has been deleted' |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + return data |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * |
| 53 | + * @param UserId |
| 54 | + * @param token |
| 55 | + */ |
| 56 | + public static async findByTokenUser(UserId: string, token: string) { |
| 57 | + const data = await Session.findOne({ where: { UserId, token } }) |
| 58 | + |
| 59 | + if (!data) { |
| 60 | + throw new ResponseError.NotFound( |
| 61 | + 'the login session has ended, please re-login' |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + return data |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * |
| 70 | + * @param formData |
| 71 | + */ |
| 72 | + public static async create(formData: SessionAttributes) { |
| 73 | + const value = useValidation(schema.create, formData) |
| 74 | + const data = await Session.create(value) |
| 75 | + |
| 76 | + return data |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * |
| 81 | + * @param id |
| 82 | + * @param formData |
| 83 | + */ |
| 84 | + public static async update(id: string, formData: SessionAttributes) { |
| 85 | + const data = await this.getOne(id) |
| 86 | + |
| 87 | + const value = useValidation(schema.create, { |
| 88 | + ...data.toJSON(), |
| 89 | + ...formData, |
| 90 | + }) |
| 91 | + |
| 92 | + await data.update(value || {}) |
| 93 | + |
| 94 | + return data |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * |
| 99 | + * @param UserId |
| 100 | + * @param token |
| 101 | + */ |
| 102 | + public static async deleteByTokenUser(UserId: string, token: string) { |
| 103 | + await Session.destroy({ where: { UserId, token } }) |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * |
| 108 | + * @param id - Force Delete |
| 109 | + */ |
| 110 | + public static async delete(id: string) { |
| 111 | + const data = await this.getOne(id) |
| 112 | + await data.destroy() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default SessionService |
0 commit comments