Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Shaharsol/admin edit offer title #374

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions scripts/src/admin/services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, RequestHandler, Response } from "express";

import { Application, ApplicationConfig, AppOffer } from "../models/applications";
import { Cap, Offer, OfferContent, PollAnswer } from "../models/offers";
import { Cap, Offer, OfferContent, PollAnswer, OfferMeta } from "../models/offers";
import { GradualMigrationUser, User, WalletApplication, Wallet as UserWallets } from "../models/users";
import { OpenOrderStatus, Order, OrderContext } from "../models/orders";
import { IdPrefix, isNothing } from "../utils/utils";
Expand Down Expand Up @@ -107,6 +107,10 @@ async function offerToHtml(offer: Offer, appOffer?: AppOffer): Promise<string> {
return `<input type="number" onchange="submitData('/offers/${ offer.id }', { amount: Number(this.value) })" value="${ offer.amount }"/>`;
}

function getTitleElement() {
return `<input type="text" onchange="submitData('/offers/${ offer.id }', { title: this.value, is_meta: true })" value="${ offer.meta.title }"/>`;
}

const offerContent = ((await OfferContent.get(offer.id)) || { contentType: "poll", content: "{}" });
const offerIdHtml = offerContent.contentType === "coupon" ? offer.id : `<a onclick="overlayOn(this.dataset.content, '${ offer.id }')" data-content="${ escape(offerContent.content) }">${ offer.id }</a>`;
return `<tr class='offer-row'>
Expand All @@ -116,7 +120,7 @@ async function offerToHtml(offer: Offer, appOffer?: AppOffer): Promise<string> {
<td>${ offer.name }</td>
<td>${ offer.type }</td>
<td>${ getAmountElement() }</td>
<td>${ offer.meta.title }</td>
<td>${ getTitleElement() }</td>
<td>${ offer.meta.description }</td>
<td><img src="${ offer.meta.image }"/></td>
<td>${ total() }</td>
Expand Down Expand Up @@ -520,12 +524,18 @@ export async function changeAppOffer(body: { cap: Cap }, params: { app_id: strin

export type ChangeOfferData = Partial<Offer> & {
content: string;
is_meta?: boolean;
[key: string]: string | boolean | undefined;
};

function isInOffer(key: string, offer: Offer): key is keyof Offer {
return key in offer;
}

function isInMeta(key: string, offer: Offer): key is keyof Offer {
return key in offer.meta;
}

export async function changeOffer(body: ChangeOfferData, params: { offer_id: string }, query: any): Promise<any> {
const offer = await Offer.findOneById(params.offer_id);
console.log("changeOffer:", offer, "with:", body);
Expand All @@ -545,6 +555,10 @@ export async function changeOffer(body: ChangeOfferData, params: { offer_id: str
offer[key] = body[key]!;
console.log("updating:", key, "with:", body[key]);
}
if (body.is_meta && isInMeta(key, offer)){
offer.meta[key] = body[key]!.toString();
console.log("updating meta:", key, "with:", body[key]);
}
});

await offer.save();
Expand Down
1 change: 1 addition & 0 deletions scripts/src/models/offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type OfferMeta = {
image: string;
description: string;
order_meta: OrderMeta;
[key: string]: string | OrderMeta;
};

export type Cap = {
Expand Down