+
Skip to content

feat: redeemed coupon information #6816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions packages/api-plugin-promotions-coupons/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default async function register(app) {
name: "CouponLogs",
indexes: [
[{ couponId: 1 }],
[{ orderId: 1 }],
[{ promotionId: 1 }],
[{ couponId: 1, accountId: 1 }, { unique: true }]
]
Expand Down
12 changes: 12 additions & 0 deletions packages/api-plugin-promotions-coupons/src/queries/couponLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @summary return a single coupon log based on shopId and _id
* @param {Object} context - the application context
* @param {String} shopId - The id of the shop
* @param {String} _id - The unencoded id of the coupon log
* @return {Object} - The coupon log or null
*/
export default async function couponLog(context, { shopId, _id }) {
const { collections: { CouponLogs } } = context;
const singleCouponLog = await CouponLogs.findOne({ shopId, _id });
return singleCouponLog;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @summary return a single coupon log based on shopId and _id
* @param {Object} context - the application context
* @param {String} params.orderId - The order id of the coupon log
* @return {Object} - The coupon log or null
*/
export default async function couponLogByOrderId(context, { orderId }) {
const { collections: { CouponLogs } } = context;
const singleCouponLog = await CouponLogs.findOne({ orderId });
return singleCouponLog;
}
34 changes: 34 additions & 0 deletions packages/api-plugin-promotions-coupons/src/queries/couponLogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @summary return a possibly filtered list of coupon logs
* @param {Object} context - The application context
* @param {String} shopId - The shopId to query for
* @param {Object} filter - optional filter parameters
* @return {Promise<Array<CouponLogs>>} - A list of coupon logs
*/
export default async function couponLogs(context, shopId, filter) {
const { collections: { CouponLogs } } = context;

const selector = { shopId };

if (filter) {
const { couponId, promotionId, orderId, accountId } = filter;

if (couponId) {
selector.couponId = couponId;
}

if (promotionId) {
selector.promotionId = promotionId;
}

if (orderId) {
selector.orderId = orderId;
}

if (accountId) {
selector.accountId = accountId;
}
}

return CouponLogs.find(selector);
}
8 changes: 7 additions & 1 deletion packages/api-plugin-promotions-coupons/src/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import coupon from "./coupon.js";
import coupons from "./coupons.js";
import couponLog from "./couponLog.js";
import couponLogs from "./couponLogs.js";
import couponLogByOrderId from "./couponLogByOrderId.js";

export default {
coupon,
coupons
coupons,
couponLog,
couponLogs,
couponLogByOrderId
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
couponLog: (order, _, context) => context.queries.couponLogByOrderId(context, order.orderId)
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @summary query the coupons collection for a single coupon log
* @param {Object} _ - unused
* @param {Object} args - an object of all arguments that were sent by the client
* @param {String} args.shopId - Shop id of the coupon
* @param {Object} context - an object containing the per-request state
* @returns {Promise<Object>} A coupon log record or null
*/
export default async function couponLog(_, args, context) {
const { input } = args;
const { shopId, _id } = input;
await context.validatePermissions("reaction:legacy:promotions", "read", { shopId });
return context.queries.couponLog(context, {
shopId, _id
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import getPaginatedResponse from "@reactioncommerce/api-utils/graphql/getPaginatedResponse.js";
import wasFieldRequested from "@reactioncommerce/api-utils/graphql/wasFieldRequested.js";

/**
* @summary Query for a list of coupon logs
* @param {Object} _ - unused
* @param {Object} args - an object of all arguments that were sent by the client
* @param {String} args.shopId - id of user to query
* @param {Object} context - an object containing the per-request state
* @param {Object} info Info about the GraphQL request
* @returns {Promise<Object>} CouponLogs
*/
export default async function couponLogs(_, args, context, info) {
const { shopId, filter, ...connectionArgs } = args;
await context.validatePermissions("reaction:legacy:promotions", "read", { shopId });
const query = await context.queries.couponLogs(context, shopId, filter);

return getPaginatedResponse(query, connectionArgs, {
includeHasNextPage: wasFieldRequested("pageInfo.hasNextPage", info),
includeHasPreviousPage: wasFieldRequested("pageInfo.hasPreviousPage", info),
includeTotalCount: wasFieldRequested("totalCount", info)
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import coupon from "./coupon.js";
import coupons from "./coupons.js";
import couponLog from "./couponLog.js";
import couponLogs from "./couponLogs.js";

export default {
coupon,
coupons
coupons,
couponLog,
couponLogs
};
2 changes: 2 additions & 0 deletions packages/api-plugin-promotions-coupons/src/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Promotion from "./Promotion/index.js";
import Mutation from "./Mutation/index.js";
import Query from "./Query/index.js";
import Order from "./Order/index.js";

export default {
Order,
Promotion,
Mutation,
Query
Expand Down
110 changes: 110 additions & 0 deletions packages/api-plugin-promotions-coupons/src/schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,41 @@ type Coupon {
discountId: ID
}

type CouponLog {
_id: ID!

"The shop ID"
shopId: ID!

"The coupon ID"
couponId: ID!

"The order ID"
orderId: ID

"The promotion ID"
promotionId: ID!

"The coupon owner ID"
accountId: ID

"The coupon code"
usedCount: Int

"The time the coupon was used"
createdAt: Date
}

extend type Promotion {
"The coupon code"
coupon: Coupon
}

extend type Order {
"The coupon log for this order that was applied"
couponLog: CouponLog
}

"Input for the applyCouponToCart mutation"
input ApplyCouponToCartInput {

Expand Down Expand Up @@ -138,6 +168,28 @@ input CouponFilter {
isArchived: Boolean
}

input CouponLogQueryInput {
"The unique ID of the coupon log"
_id: String!

"The unique ID of the shop"
shopId: String!
}

input CouponLogFilter {
"The coupon ID"
couponId: ID

"The related promotion ID"
promotionId: ID

"The orderId"
orderId: ID

"The account ID of the user who is applying the coupon"
accountId: ID
}

"Input for the removeCouponFromCart mutation"
input RemoveCouponFromCartInput {

Expand Down Expand Up @@ -206,6 +258,32 @@ type CouponConnection {
totalCount: Int!
}

"A connection edge in which each node is a `CouponLog` object"
type CouponLogEdge {
"The cursor that represents this node in the paginated results"
cursor: ConnectionCursor!

"The coupon log node"
node: CouponLog
}

type CouponLogConnection {
"The list of nodes that match the query, wrapped in an edge to provide a cursor string for each"
edges: [CouponEdge]

"""
You can request the `nodes` directly to avoid the extra wrapping that `NodeEdge` has,
if you know you will not need to paginate the results.
"""
nodes: [CouponLog]

"Information to help a client request the next or previous page"
pageInfo: PageInfo!

"The total number of nodes that match your query"
totalCount: Int!
}

extend type Query {
"Get a coupon"
coupon(
Expand Down Expand Up @@ -238,6 +316,38 @@ extend type Query {

sortOrder: String
): CouponConnection

"Get a coupon log"
couponLog(
input: CouponLogQueryInput
): CouponLog

"Get list of coupon logs"
couponLogs(
"The coupon ID"
shopId: ID!

"Return only results that come after this cursor. Use this with `first` to specify the number of results to return."
after: ConnectionCursor

"Return only results that come before this cursor. Use this with `last` to specify the number of results to return."
before: ConnectionCursor

"Return at most this many results. This parameter may be used with either `after` or `offset` parameters."
first: ConnectionLimitInt

"Return at most this many results. This parameter may be used with the `before` parameter."
last: ConnectionLimitInt

"Return only results that come after the Nth result. This parameter may be used with the `first` parameter."
offset: Int

filter: CouponLogFilter

sortBy: String

sortOrder: String
): CouponLogConnection
}

extend type Mutation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const Coupon = new SimpleSchema({

export const CouponLog = new SimpleSchema({
"_id": String,
"shopId": String,
"couponId": String,
"promotionId": String,
"orderId": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export default async function updateOrderCoupon(context, order) {
if (!couponLog) {
await CouponLogs.insertOne({
_id: Random.id(),
shopId: order.shopId,
couponId,
orderId: order._id,
promotionId: promotion._id,
accountId: order.accountId,
createdAt: new Date(),
usedCount: 1
usedCount: 1,
createdAt: new Date()
});
continue;
}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载