diff --git a/prisma/seed.ts b/prisma/seed.ts index ddcbc8f..452329c 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -3,6 +3,7 @@ import {PrismaClient} from "@prisma/client"; import {EncryptionService} from "../src/services/encryption.service"; import * as dotenv from "dotenv"; +import {getTipsSeed} from "./seeds/tips.seed"; dotenv.config(); @@ -69,15 +70,17 @@ async function main(){ }, }); + const tipsSeed = await getTipsSeed(); const tips = []; - for (let i = 1; i <= 31; i++){ + for (let i = 0; i < tipsSeed.length; i++){ tips.push(await prisma.tips.upsert({ - where: {id: i}, + where: {id: i + 1}, update: {}, create: { - id: i, - tips: `Tip ${i}`, - order: i, + id: i + 1, + tips: tipsSeed[i].tips, + author: tipsSeed[i].author || null, + order: i + 1, }, })); } diff --git a/prisma/seeds/tips.seed.ts b/prisma/seeds/tips.seed.ts new file mode 100644 index 0000000..bd0c83b --- /dev/null +++ b/prisma/seeds/tips.seed.ts @@ -0,0 +1,217 @@ +export async function getTipsSeed(){ + return [ + { + tips: "Create a life you love living.", + }, + { + tips: "The purpose of our lives is to be happy.", + author: "Dalai Lama" + }, + { + tips: "The biggest adventure you can take is to live the life of your dreams.", + author: "Oprah Winfrey" + }, + { + tips: "Happiness is not by chance, but by choice.", + author: "Jim Rohn" + }, + { + tips: "The only way to do great work is to love what you do.", + author: "Steve Jobs" + }, + { + tips: "The meaning of life is to give life meaning.", + author: "Ken Hudgins" + }, + { + tips: "Life is what happens when you're busy making other plans.", + author: "Allen Saunders" + }, + { + tips: "Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful.", + author: "Albert Schweitzer" + }, + { + tips: "In the end, it's not the years in your life that count. It's the life in your years.", + author: "Abraham Lincoln" + }, + { + tips: "The future belongs to those who believe in the beauty of their dreams.", + author: "Eleanor Roosevelt" + }, + { + tips: "Life is really simple, but we insist on making it complicated.", + author: "Confucius" + }, + { + tips: "Life is not about finding yourself. Life is about creating yourself.", + author: "George Bernard Shaw" + }, + { + tips: "Life is not measured by the number of breaths we take, but by the moments that take our breath away.", + author: "Maya Angelou" + }, + { + tips: "Don’t count the days, make the days count.", + author: "Muhammad Ali" + }, + { + tips: "The purpose of life is to live it, to taste experience to the utmost, to reach out eagerly and without fear for newer and richer experience.", + author: "Eleanor Roosevelt" + }, + { + tips: "Life is either a daring adventure or nothing at all.", + author: "Helen Keller" + }, + { + tips: "Life is short, and it's up to you to make it sweet.", + author: "Sarah Louise Delany" + }, + { + tips: "Life is a journey, not a destination.", + author: "Ralph Waldo Emerson" + }, + { + tips: "Life isn't about waiting for the storm to pass, it's about learning to dance in the rain.", + author: "Vivian Greene" + }, + { + tips: "Live as if you were to die tomorrow. Learn as if you were to live forever.", + author: "Mahatma Gandhi" + }, + { + tips: "Life’s most persistent and urgent question is, 'What are you doing for others?'", + author: "Martin Luther King Jr." + }, + { + tips: "Life is 10% what happens to us and 90% how we react to it.", + author: "Charles R. Swindoll" + }, + { + tips: "The purpose of our lives is to add value to the people of this generation and those that follow.", + author: "Buckminster Fuller" + }, + { + tips: "Life is about making an impact, not making an income.", + author: "Kevin Kruse" + }, + { + tips: "Life is short, but it's long enough to make a difference.", + author: "Barry Long" + }, + { + tips: "Life is like riding a bicycle. To keep your balance, you must keep moving.", + author: "Albert Einstein" + }, + { + tips: "Life is a series of natural and spontaneous changes. Don't resist them; that only creates sorrow. Let reality be reality. Let things flow naturally forward in whatever way they like.", + author: "Lao Tzu" + }, + { + tips: "The best way to predict your future is to create it.", + author: "Peter Drucker" + }, + { + tips: "The only impossible journey is the one you never begin.", + author: "Tony Robbins" + }, + { + tips: "Life is not a problem to be solved, but a reality to be experienced.", + author: "Søren Kierkegaard" + }, + { + tips: "Life is a balance between holding on and letting go.", + author: "Rumi" + }, + { + tips: "Cherish every moment, for they paint the canvas of your life." + }, + { + tips: "Life is a symphony; compose it with purpose and passion." + }, + { + tips: "The key to fulfillment lies in the harmony of gratitude and contentment." + }, + { + tips: "Embrace the rhythm of change, for it orchestrates growth." + }, + { + tips: "Live boldly, for courage is the artist's brush that colors your destiny." + }, + { + tips: "Each sunrise gifts you a new canvas; paint it with kindness and hope." + }, + { + tips: "The garden of life blooms with resilience and perseverance." + }, + { + tips: "Find serenity in the melody of simplicity." + }, + { + tips: "Illuminate your path with the light of self-discovery and acceptance." + }, + { + tips: "Life's masterpiece is etched in the moments of genuine connection and love." + }, + { + tips: "Dance joyously in the rain of challenges; it waters the seeds of resilience." + }, + { + tips: "Forge your destiny with the hammer of determination and the anvil of resilience." + }, + { + tips: "Life's symphony is composed of harmony, balance, and self-reflection." + }, + { + tips: "Bloom wherever life plants you; your growth is a testament to resilience." + }, + { + tips: "Create your legacy through kindness, empathy, and unwavering authenticity." + }, + { + tips: "Nurture the garden of your soul; its blossoms are self-love and acceptance." + }, + { + tips: "Life's compass points towards purpose; navigate it with passion and conviction." + }, + { + tips: "In the mosaic of life, every broken piece tells a story of resilience and growth." + }, + { + tips: "Seek wisdom in the whispers of silence; it reveals the secrets of contentment." + }, + { + tips: "Life's melody is composed of love, laughter, and the symphony of shared moments." + }, + { + tips: "Every setback is a brushstroke; paint your resilience in vibrant colors." + }, + { + tips: "Life's journey is the potter's wheel; mold it with courage and grace." + }, + { + tips: "Embrace the winds of change; they carry the seeds of growth and transformation." + }, + { + tips: "The tapestry of life weaves resilience, kindness, and relentless determination." + }, + { + tips: "Life is an unfinished novel; write each chapter with purpose and passion." + }, + { + tips: "Sow the seeds of kindness, for they bear the fruits of everlasting joy." + }, + { + tips: "Every obstacle is a stepping stone; climb the stairs of resilience and determination." + }, + { + tips: "Life's compass always points towards gratitude and compassion." + }, + { + tips: "Embrace solitude; it is where self-discovery and introspection blossom." + }, + { + tips: "In life's grand orchestra, resilience plays the most beautiful symphony." + } + ]; +} diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index beb50d4..a82c5ec 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -3,7 +3,7 @@ import { ConflictException, ForbiddenException, Injectable, InternalServerErrorException, - NotFoundException, PreconditionFailedException, UnauthorizedException + NotFoundException, PreconditionFailedException } from "@nestjs/common"; import {EncryptionService} from "../services/encryption.service"; import {AtRtResponse} from "./models/responses/atrt.response"; diff --git a/src/tasks/tasks.module.ts b/src/tasks/tasks.module.ts index 3ab7dd2..c3f3f93 100644 --- a/src/tasks/tasks.module.ts +++ b/src/tasks/tasks.module.ts @@ -5,9 +5,11 @@ import {UserCleanupTask} from "./user-cleanup.task"; import {SecretsRotationTask} from "./secrets-rotation.task"; import {UsersModule} from "../users/users.module"; import {SecretsModule} from "../secrets/secrets.module"; +import {TipsModule} from "../tips/tips.module"; +import {TipsRandomizingTask} from "./tips-randomizing.task"; @Module({ - providers: [TokenCleanupTask, UserCleanupTask, SecretsRotationTask], - imports: [ServicesModule, UsersModule, SecretsModule], + providers: [TokenCleanupTask, UserCleanupTask, SecretsRotationTask, TipsRandomizingTask], + imports: [ServicesModule, UsersModule, SecretsModule, TipsModule], }) export class TasksModule{} diff --git a/src/tasks/tips-randomizing.task.ts b/src/tasks/tips-randomizing.task.ts new file mode 100644 index 0000000..706e6ba --- /dev/null +++ b/src/tasks/tips-randomizing.task.ts @@ -0,0 +1,21 @@ +import {Injectable, Logger} from "@nestjs/common"; +import {Cron} from "@nestjs/schedule"; +import {TipsService} from "../tips/tips.service"; + + +@Injectable() +export class TipsRandomizingTask{ + + private readonly logger = new Logger(TipsRandomizingTask.name); + + constructor( + private readonly tipsService: TipsService, + ){} + + @Cron("0 0 0 1 * *") + async handleCron(){ + this.logger.log("Randomizing tips order"); + await this.tipsService.randomizeTipsOrder(); + this.logger.log("Tips order randomized"); + } +} diff --git a/src/tips/tips.module.ts b/src/tips/tips.module.ts index 3c40c40..06a193e 100644 --- a/src/tips/tips.module.ts +++ b/src/tips/tips.module.ts @@ -7,6 +7,7 @@ import {UsersModule} from "../users/users.module"; @Module({ controllers: [TipsController], providers: [TipsService], - imports: [ServicesModule, UsersModule] + imports: [ServicesModule, UsersModule], + exports: [TipsService] }) export class TipsModule{} diff --git a/src/tips/tips.service.ts b/src/tips/tips.service.ts index 41aed6c..6507b5c 100644 --- a/src/tips/tips.service.ts +++ b/src/tips/tips.service.ts @@ -1,6 +1,8 @@ import {Injectable} from "@nestjs/common"; import {PrismaService} from "../services/prisma.service"; +import {TipEntity} from "./models/entities/tip.entity"; +// noinspection TypeScriptValidateJSTypes @Injectable() export class TipsService{ @@ -10,10 +12,51 @@ export class TipsService{ async getTipOfTheDay(){ const day = new Date().getDay(); - return this.prismaService.tips.findFirst({ + return this.prismaService.tips.findUnique({ where: { order: day } }); } + + async randomizeTipsOrder(){ + const tips: TipEntity[] = await this.prismaService.tips.findMany(); + const tipsIds: number[] = tips.map(tip => tip.id); + const randomizedTipsIds: number[] = this.randomizeArray(tipsIds); + const maxId: number = Math.max(...tipsIds); + await this.prismaService.$transaction(async(tx) => { + for(const tip of tips){ + await tx.tips.update({ + where: { + id: tip.id + }, + data: { + order: maxId + tip.id + 10 + } + }); + } + for(const tip of tips){ + await tx.tips.update({ + where: { + id: tip.id + }, + data: { + order: randomizedTipsIds.shift() + } + }); + } + }); + } + + private randomizeArray(array: any[]){ + let currentIndex = array.length, temporaryValue: any, randomIndex: any; + while(0 !== currentIndex){ + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + temporaryValue = array[currentIndex]; + array[currentIndex] = array[randomIndex]; + array[randomIndex] = temporaryValue; + } + return [...array]; + } }